Microsoft Interview Question for Developer Program Engineers


Country: India




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

public static void removeSpace(char[] string) {
		if (string == null || string.length == 0) {
			return;
		}

		int p1 = 0;
		int p2 = 0;

		do {
			while (p2 < string.length && string[p2] == ' ') {
				p2++;
			}
			if (p2 == string.length) {
				break;
			}
			string[p1++] = string[p2++];
		} while (p1 < string.length && p2 < string.length);

		for (int p = p1; p < string.length; p++) {
			string[p] = 0;
		}

		for (char c : string) {
			System.out.print(c + " ");
		}
		
		System.out.println();
	}

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

I think this must be used as a screening question

A straightforward solution in C is

char* removeSpaces(char* str)
{
    if (str)
    {
        char* pWrite = str;
        for (char* p=str; *p; p++)
            if (*p!=' ')
                *(pWrite++) = *p;
        *pWrite = 0;
    }
    return str;
}

Note that this can be made a little faster by partially unrolling the loop, skipping over characters before the first space since they don't need to be copied.

Now, if you know a language which has advanced meta-programming, you can follow-up the simple solution above by showing a "lazy evaluation" solution, such as the following code in D. This is O(m) instead of O(n), where m is the number of spaces at the start of the string.

auto removeSpaces(E)(E[] input)
{
    struct Result
    {
        private E[] source = input;
        private void skipSpaces() 
        { 
            while(!source.empty && source.front==' ')
                source.popFront(); 
        }

        this() { skipSpaces(); }
        @property E front() { return source.front; }
        @property bool empty () { return source.empty; }
        void popFront() 
        { 
            source.popFront(); 
            skipSpaces(); 
        }        
    };

    Result r;
    return r;
}

- Leif E Glacer August 08, 2013 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

This is sample to see I can actually preserve the whitespaces

- asu June 03, 2015 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 3 vote

fun(char *a)
{
for(i=j=0;a[i];i++)
if(a[i]!=' ')a[j++]=a[i];
a[j]='\0';
}

- xyz July 29, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

what is wrong with this code pls explain

- xyz July 29, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

logic correct, but question given than without shifting... otherwise it is very easy question not a MS question

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

@Anonymous talking about easy/MS: What does the question exactly mean? Care to elaborate?

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

you are not allowed to shift the characters from one location to another. here if there is space you are shifting the character from i to i-1 location... so total length of the string will be decreased but expected is should be of same length.

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

What does not shifting even mean? Can I shift from i+100 to i?

Ridiculous question.

A simpler way would be to ask for an O(n) time, in-place algorithm. Which I suspect it was, but OP managed to screw up.

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

one possible but fullish way is to use linked list instead of char array...... here we can delete the node with whitespace....

not a answer but a possible way :)

- harry8906 July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

This solution will work

There is an O(n²) solution that uses shifting. I suspect the "without shifting" was a hint given by the interviewer to get the O(n) solution instead.

- Leif E Glacer August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

class string // Just deleting character
{
public static void main(String[] args)
{
String x="teja is a good boy";
x=x.replaceAll(" ","");
System.out.println(x);
}
}

- TEJA July 30, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think the question implies "in-place", but it would be good to clarify with the interviewer before proceeding

Also, using stock library functionality during an interview is often not allowed

- Leif E Glacer August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
-1
of 1 vote

I think in place and without shifting is not possible together...any clues?

- Victor July 31, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

std::string remBlank(std::string input){
std::string newstring;
for(int i=0;i<input.length();i++){
if(input.at(i)==32){
newstring+="%20"; }
else{newstring+=input.at(i);} }
return newstring;}

- PRAJWALPRAS August 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

What about this code.... correct me if i am wrong...!!
int main()
{
char s[] = "how are you friends";
int i=0;
while(s[i] != '\0')
{
if(s[i] == ' ' && s[i+1]==' ')
{ s[i+1] = '\b'; i++; }
else if(s[i]== ' ') s[i]='\a';
i++;
}
printf("%s\n", s);
}

- Atul August 01, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-1
of 1 vote

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
string str = "how are you";
int n = str.Length;
int i=0;

char[] s = str.ToCharArray();

Console.WriteLine("Original string : " + str);
for (i = 0; i < n; i++)
{
if (s[i] == ' ')
s[i] = '\a'; // ascii for bell

}

Console.Write("String w/o space : ");
for (i = 0; i < n; i++)
Console.Write(s[i]);

Console.Read();
}
}
}

- Amrita August 16, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
-2
of 6 vote

char s[] = "how are     you";
	int n = strlen(s);
	int i=0;
	while(s[i] != '\0')
	{
		if(s[i] == ' ')
			s[i] = 7; // ascii for bell
		i++;
	}
	cout << s << endl;

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

instead of 7 you can use character "audible" i.e..'\a'...
when display receives this character it will not get printed
s[i] = '\a'; // ascii for audible

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

That's probably not what the interviewer is asking for

- Leif E Glacer August 08, 2013 | Flag
Comment hidden because of low score. Click to expand.
-2
of 2 vote

How about we fill the white spaces with null character('\0') and concatenate the sub strings.. plzz comment

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

concatenation of 2 strings requires O(m+n) extra space. so this approach is not inplace.

- sandeep July 30, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No we are not using extra space. we are splitting the string into substrings and concatenating at the same time.

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

ok...my bad..

- Anonymous July 30, 2012 | Flag


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