Microsoft Interview Question for Software Engineer / Developers






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

void RemoveWords(char c)
{
string str = "HELLO WORLD HELLO TO THIS DAY OF THE HAPPY WEEK";
char *s = new char[str.size() + 1];
strcpy(s, str.c_str());

for (int i=0; i<str.size(); i++)
{
if ((i == 0 || s[i-1] == ' ') && s[i] == c)
{
while (s[i] != ' ' && i<str.size())
{
s[i] = ' ';
i++;
}
}
}

int actual_pos=0, new_pos=0;
//for (actual_pos = 0; str[actual_pos] != ' ' && actual_pos < str.size(); actual_pos++);

for (int i=1; i<str.size(); i++)
{
while (s[actual_pos-1] == ' ' && s[actual_pos] == ' ' && actual_pos<str.size())
actual_pos++;

s[new_pos++] = s[actual_pos++];

}

cout << s;

}

- Anonymous December 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Single Pass soln:

void RemoveWordsNoSpace(char c)
{
    int actual_pos=0, new_pos=0;
    string str = "HELLO WORLD HELLO TO THIS DAY OF THE HAPPY WEEK";
    char *s = new char[str.size() + 1];
    strcpy(s, str.c_str());
    
    for (int i=0; i<str.size(); i++)
    {
        if ((i == 0 || s[i-1] == ' ') && s[i] == c)
        {
            while (s[i] != ' ' && i<str.size())
            {
                actual_pos++;
                i++;
            }
            new_pos--;
        }

        s[new_pos++] = s[actual_pos++];
    }
}

- Anonymous December 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

I can think of an O(n) solution which makes 2 passes over the list. Did they have any specific requirements ?

- balchandra December 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Just extract those all strings which is not starting from c.This can be in O(N)

- nks December 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@Balchandra
I proposed them of 2 passes but I eventually gave him one pass solution.

I will paste the code sometime later.

- Aatish December 23, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<iostream>
#include<string>
using namespace std;

int main()
{
	char inp[100];
	cout<<"Enter the input line = ";
	gets(inp);
	char c = ' ';
	int index = 0;
	cout << "Enter characters to be eliminated = ";
	cin >> c;
	for(int i=0;inp[i];i++)
	{
		if(inp[i] == c)
		{
			while((inp[i] != ' ')&&(inp[i] != '\0'))
			{
				i++;
			}
		i--;	
		}
		else
		{
			inp[index++] = inp[i];
		}
	}
	inp[index] = '\0';
	cout << inp;
	return 0;
}

- Gajanan December 24, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think thr is a bug in ur code.. its that if a word doesnt start with 'c' but contains 'c' then ur code wont print part of this string after 'c'.

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

a python implementation :

>>> def f(str,k):
beg,write=True,True
"""beg indicates whether we have reached the beginning of a new word.
write indicates whether the current character has to be written to
output."""
res=''
"res stores the result"
for c in str:
if beg:
write= not(c=='t')
beg=(c==' ')
if write:
res+=c
return res

>>> f('all the words starting with the letter t will be removed','t')
'all words starting with letter will be removed'
>>>

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

correction:
write= not(c=='t') should be write= not(c==k)

- Anonymous December 30, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

void RemoveWords(char *str, char c)
{
	int len = str.length;
	int cur = 0;
	int prev = 0;
	while(cur < len)
	{
		if((strArr[cur] != c)
		 || (strArr[cur] == c && cur > 0 && strArr[cur - 1] != ' '))
		{
			if(strArr[cur] != strArr[prev]
			{
				strArr[prev] = strArr[cur];
			}
			cur++; 
			prev++;
		}
		else
		{
			while(strArr[cur] != ' ' && cur < len)
			{
				cur++;
			}
		}		
		
	}
	str[prev] = '\0';

}

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

hey Aatish were u interviewed in India?Is Microsoft Hiring in India?

- dream January 20, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ptr=strtok(str," ");
while(ptr!=NULL)
{
if(ptr[0]=='H')
{
ptr=strtok(NULL," ");
continue;
}
cout<<ptr<<endl;
ptr=strtok(NULL," ");

}

- intuidev January 21, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Eliminates multiple white spaces between words

void removeWords(char* string, char c){
	if(string == 0)
		return;
	
	int start = 0, current = 0;
	
	while(1){
		int discardToken = (string[current] == c);
		while((string[current] != '\0') && (string[current] != ' ')){
			if(discardToken)
				current++;
			else
				string[start++] = string[current++];
		}
		if((start > 0) & (string[start - 1] != string[current]))
			string[start++] = string[current];
		if(string[current] == '\0')
			return;
		else{
			current++;
		}
	}
}

- Anonymous March 27, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Flawless code ... Enjoy!!
Time O(n) .... Space O(1)

Below code also takes care of multiple space characters between two words...

void removeWords(char * str, char ch)
{
	if(!str)
		return;
	int i = 0, j = 0;
	while(str[j])
	{
		if(str[j] == ch)
		{
			while(str[j] && str[j] != ' ')
				j++;
			while(str[j] && str[j] == ' ')
				j++;
		}
		else
		{
			while(str[j] && str[j] != ' ')
				str[i++] = str[j++];
			while(str[j] && str[j] == ' ')
				str[i++] = str[j++];
		}
	}
	str[i] = '\0';
}

- Avinash September 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public class RemoveWordsStartingWithAChar 
{
	/**
	 * @param args
	 */
	static String[] cases = new String[]{"HELLO WORLD HAT", "HE WAS GOING TO THE HUT", "HI!"};
	static char c = 'H';
	
	public static void main(String[] args) 
	{
		for(String case_ : cases)
		{
			System.out.println("Original sentence: "+case_);
			System.out.println("Modified sentence: "+change(case_));
		}

	}
	
	private static String change(String case_)
	{	
		StringBuilder modified_case = new StringBuilder();
		
		boolean addThisWord = true;
		for(int i=0; i<case_.length(); i++)
		{
			if(i==0 || case_.charAt(i-1)==' ')
			{	
				addThisWord = true;
				if(case_.charAt(i)==c)
				{
					addThisWord = false;
				}
			}
			
			if(addThisWord)
				modified_case.append(case_.charAt(i));
			
		}
		return modified_case.toString().trim();
	}

}

- Devesh March 12, 2016 | 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