Microsoft Interview Question for Software Engineer / Developers






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

I am guessing the special char '!' is used as a pivot for reversing the words before and after it. i.e. ABCD E!FG HI! => E DCBA!IH GF!

This is similar to the conventional reverseWord problem.

#define SPECIAL_CHAR '!'

void reverseWords(char str[]) {
if (!str) return;

int cur=0;
int start=0;

while (str[cur]) {
if (str[cur]==SPECIAL_CHAR) {
reverse(str, start, cur-1);
start=cur+1;
}
cur++;
}

start=cur=0;

while (str[cur]) {
if (str[cur]==SPECIAL_CHAR || str[cur]==' ') {
reverse(str, start, cur-1);
start=cur+1;
}
cur++;
}

return;
}

void reverse(char *str, int beg, int end) {
while (beg < end) {
char temp = str[beg];
str[beg] = str[end];
str[end] = temp;
beg++; end--;
}
return;
}

i have a couple of test cases in mind but what does the question mean by "handle special chars"? can you give an example of special char?

- nunbit romance June 11, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It means it should be able to handle ascii and unicode characters as well

- Vams December 04, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Your guess is wrong..
Modified the above solution

#define SPECIAL_CHAR '!' 

void reverseWords(char str[]) { 
   if (!str) return; 

   int cur=0; 
   int start=0; 

   while (str[cur]) { 
       if (str[cur]==SPECIAL_CHAR || str[cur]==' ') { 
            reverse(str, start, cur-1); 
            start=cur+1; 
   } 
   cur++; 
} 

return; 
} 

void reverse(char *str, int beg, int end) { 
     while (beg < end) { 
        char temp = str[beg]; 
        str[beg] = str[end]; 
        str[end] = temp; 
        beg++; end--; 
} 
return; 
}

- Vamsi December 06, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Right Solution --Ignore above solution

#define SPECIAL_CHAR '!'

void reverseWords(char str[]) {
	if (!str) return;

   	int cur=0, start=0;
   	while (str[cur]) {
    	if (str[cur]==SPECIAL_CHAR || str[cur]==' ') {
			if(str[cur]=SPECIAL_CHAR
				cur=cur-1;
            reverse(str, start, cur);
            start=cur+1;
   		}
   		cur++;
	}
}

void reverse(char *str, int beg, int end) {
    while (beg < end) {
  	   char temp = str[beg];
       str[beg] = str[end];
       str[end] = temp;
       beg++; end--;
	}

}

- Vamsi December 06, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

It seems nobody is interested in testing and hence the test cases.

There are the following test cases I have come up with

"Empty string"
!!! !!!
!!!a!!!
!ab!!
!!asda!!sdf!!sadf
asdf!
asdf

- AD September 30, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

bool convert(char* str)
{
if(!str||!*str) return false;
char *p1=str;
while(*p1=='!'||*p1==' ') p1++;
char *p2=p1+1;
while(*p1)
{
while(*p2!=' '&&*p2!='!'&&*p2!='\0')
p2++;
rev(p1,p2-1);
while(*p2==' '||*p2=='!')
p2++;
p1=p2;
}
return true;
}

rev(char* p1, char* p2)
{
while(p1<p2)
{
char temp=*p1;
*p1=*p2;
*p2=temp;
p1++;p2--;
}
}

- winia November 01, 2008 | 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