Microsoft Interview Question for Tech Leads


Team: Bing
Country: India
Interview Type: In-Person




Comment hidden because of low score. Click to expand.
2
of 2 vote

whitespacecount=0;
string= given string;
Step1: Iterate thro the string by each and every character.
If( string[i]==' ')
{
If (IsNotSplCharacter(string[i-1]))
whitespacecount++;
}
else
string[i-whitespacecount]= string[i];
Complexity is O(n).

- dhineshkumar007 December 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Are you sure this is O(n)? I am asking because I know that you can't access the char from position j in a string using : str[j]. And you can't do str[j] = str[i], you will have to do : str = str.substring(0,j) +str.charAt(i)+str.substring(j+1). This will take O(n) . Including this in an iteration throw each character gives an O(n^2) algorithm.

- Kamy December 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

the algo is not o(n) , it must be O (n^2)as is trying to shift all the data left , after encountering multiple spaces

- nachi December 27, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Try it out this solution, it will not work, since you need to do it in the same buffer, when you copy characters to whitespace, then you need to convert the index of the characters being copied to whitespace, else it would lead to doubling of characters.

Also, the access part pointed earlier is also an issue

- mrinalkamboj January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
1
of 1 vote

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#define MAX_STRING 100
#define NEW_LINE1 printf("\n");
#define NEW_LINE2 printf("\n\n");
#define STR_LEN(x) printf("\n\nstrlen == %d", strlen(x));

void remove_whitespace(char *str1);

int main(int argc, char *argv[])
{

char *str1 = calloc(1, MAX_STRING);
char *str2 = calloc(1, MAX_STRING);

str1 = " abcd efgh ijkl mnop ";

remove_whitespace(str1);
system("PAUSE");
return 0;
}

void remove_whitespace(char *str1)
{
char *str1_local = calloc(1, MAX_STRING);
char *str2_local = calloc(1, MAX_STRING);
char *str2_final = calloc(1, MAX_STRING);
int i = 0;
strcpy(str1_local, str1);
int length = 0;

printf("\nstr1_local = %d, %s", str1_local, str1_local);
printf("\nstr2_local = %d, %s", str2_local, str2_local);
printf("\nstr2_final = %d, %s", str2_final, str2_final);
NEW_LINE2;
NEW_LINE2;

do {
str2_local = strchr(str1_local, 32);

if(str2_local) {
NEW_LINE1;
strncat(str2_final , str1_local, str2_local - str1_local);
printf("\nstr1_local = %d, %s", str1_local, str1_local);
printf("\nstr2_local = %d, %s", str2_local, str2_local);
printf("\nstr2_final = %d, %s", str2_final, str2_final);
NEW_LINE1;
str1_local = str2_local + 1;
}
else {
strncat(str2_final , str1_local, strlen(str1_local));
}
length++;

} while (str2_local);

NEW_LINE2;
puts(str2_final);
STR_LEN(str2_final);
NEW_LINE2;

free (str1_local);
free (str2_local);
free (str2_final);
}

- Anonymous December 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

to trim all whitespaces, similarly can be modified to remove any unwanted chars, modify remove_whitespace and apss another arg with the char and modify , printf s left FYI

- Anonymous December 20, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Operation of trimming the whitespace has to be done in a single buffer, we cannot use multiple buffers, I missed out this point in the question

- mrinalkamboj January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Is it necessary to do it in place? Why not read the whole string char by char and output to new variable if its non extra whitespace or valid character. O(n)

- Anon January 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yes, I missed out that important point we need to do it in a single buffer, no extra buffer should be allocated

- mrinalkamboj January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

One way of solving is given below :

- for every character in the string, check if it is a redundant space
- If it is, then keep a count of the redundant spaces encountered so far and shift the position of the current character left by "count" times

pseudo code :


function adjust_spaces(char s[])
{

redundant_spaces_count = 0;
for(int i=0;i<s.length();i++)
{
if(redundant_space(s[i]))
redundant_spaces_deleted++;
else
s[i-redundant_spaces_deleted] = s[i];
}
}

- Srikanth January 10, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

This will not work if there are more than one consecutive spaces. Suppose you have 4 spaces in between a and b ex:- a----b, when i = 5 i.e, b consecutive spaces would be 4 and 5-4 = 1 and beside a you are replacing b ab--- leaving 3 spaces to the right of b.

- Anonymous February 05, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

How about using a linked list with O(n)

Node result = new Node(); Node before = null;
for (Node character = start; character != null; character = character.next)
{
if (character.data == ' ' && before == null)
{
before = character.next; result = character.next;
}
else if (character.data == ' ')
{
before.next = character.next;
}
else
before = character;
}

- Spandy February 05, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

here it is in c# using Regex

{
Console.Write(Regex.Replace(" test man , this is not ! Where to go lamp ",@"\s+", " ").Trim())
}

- pollmaker February 14, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

void Replaces(char* str)
{
	int it =0;
	int in=0;
	bool iswhite = false;
	while( str[it] != '\0')
	{
		if( str[it] == ' ' )
			it++;
		else
			break;
	}
	while( str[it] != '\0')
	{
		if(( iswhite) && (str[it] == ' '))
		{
			it++;
		}
		else
		{
			str[in] = str[it];
			if( str[it] == ' ')
				iswhite = true;
			else
				iswhite = false;
			it++;
			in++;
		}
	}
	if( iswhite)
		str[--in] = '\0';
	else
		str[in] = '\0';
}

- Anonymous February 18, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

What's the time complexity time of my code?

public class RemoveWhiteSpace {

	public static void removeSpace(char[] string) {
		int spacePtr = 0;
		while(spacePtr < string.length && string[spacePtr] != ' '){
			spacePtr++;
		}
		int index = 0;
		for(index = spacePtr;index < string.length; index++){
			if(string[index] == ' '){
				continue;
			}else{
				swap(string,spacePtr,index);
				while(spacePtr < string.length && string[spacePtr] != ' '){
					spacePtr++;
				}
			}
		}
		while(spacePtr < string.length){
			string[spacePtr] = 0;
			spacePtr++;
		}
		for(char c:string){
			System.out.print(c);
		}
		System.out.println();
	}

	private static void swap(char[] string, int spacePtr, int index) {
		char tmp = string[spacePtr];
		string[spacePtr] = string[index];
		string[index] = tmp;
	}

	public static void main(String[] args) {
		removeSpace(" ab  bc d".toCharArray());
		removeSpace("ab ed d d".toCharArray());
	}

}

- Kevin March 02, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

trim()

- Ramesh December 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You need to implement Trim() :)

- mrinalkamboj January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think you can also use str.replaceAll(" "," ") // replace all double spaces with one space. Check if it starts or ends with a space , and delete it, if so.

- Kamy December 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You cannot use APIs, implement the working of Replace

- mrinalkamboj January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

store the string into an array: str
start with two indexes : idx=0 and k=0

while(k < str.length){
  if(str[k] == ' '){
     if(str[idx] != ' '){
        str[idx] = str[k];
        idx++;
     }
  } else { str[idx] = str[k]; idx++;}
  k++;
}

reconstruct the string from the array between positions : 0 and < idx

- Kamy January 04, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

use
String str2=str.trim() -to remove leading and trailling white spaces
now
String str3=str2.replaceAll("\\s+"," ")

- Anonymous December 20, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

You cannot use APIs, you need to implement the working of Trim and Replace APIs

- mrinalkamboj January 02, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

in above reply to the question
replaceAll replaces uneven number of white spaces with single white space
in order to remove them completely use replaceAll("\\s+","")

- Anonymous December 20, 2012 | Flag Reply


Add a Comment
Name:

Writing Code? Surround your code with {{{ and }}} to preserve whitespace.

Books

is a comprehensive book on getting a job at a top tech company, while focuses on dev interviews and does this for PMs.

Learn More

Videos

CareerCup's interview videos give you a real-life look at technical interviews. In these unscripted videos, watch how other candidates handle tough questions and how the interviewer thinks about their performance.

Learn More

Resume Review

Most engineers make critical mistakes on their resumes -- we can fix your resume with our custom resume review service. And, we use fellow engineers as our resume reviewers, so you can be sure that we "get" what you're saying.

Learn More

Mock Interviews

Our Mock Interviews will be conducted "in character" just like a real interview, and can focus on whatever topics you want. All our interviewers have worked for Microsoft, Google or Amazon, you know you'll get a true-to-life experience.

Learn More