Microsoft Interview Question for Software Engineer / Developers






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

The question is simpler than it sounds. Just call:
strstr(strcat(s1,s1), s2)

so if the string is ABCD, the concatination will make it ABCDABCD and hence any rotation will fall within the concatenated string.

- K February 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

use strncat instead of strcat....
i guess it gives segmentation fault with strcat

- Anonymous May 19, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I guess we cannot assume if it is right or left rotated...so do the operation for s1,s2 and in the opposite order i.e. s2,s1.

- Ram March 05, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

yours is good one, but,

s1+s1 is not needed. s1+ half of s1 is enough. I think,

ABCDAB is enough. is that true?

- prabhakhar May 18, 2008 | Flag
Comment hidden because of low score. Click to expand.
2
of 2 vote

ok the best solution would be as aayush said
if(s1.lenght==s2,length)
{
return strstr(strcat(s1,s1),s2);
}
else
return false;

- Anonymous June 11, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

That's a really neat solution. Good job!

- vodangkhoa February 01, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Actually before checking for strstr(strcat(s1,s1), s2)
we also need to check if s1 and s2 are of same length. Eg: strstr(strcat(s1,s1), s2) will be true even when
s1 = ABCD
s2 = BCD

- Aayush June 10, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

The above algorithm does not handle the following case:
str1 = ABCD
str2 = DABC

this should return false, whereas using above would return true.

strcat(str1, str1) = ABCDABCD
str2 = DABC <-- exists in the concatenated string.

Solution:
After concatenation, always compare from the index of half of the length of str1 to the max length of str2.

- Guru June 30, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

DABC is valid. it should return true.

if(strlen(s1) == strlen(s2))
return strstr(strcat(s1,s1),s2);
else
return false;

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

come on man.,.. read the question first.. DABC shoiuld be valid

- ghost rider April 26, 2008 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

We cant always assume that the string is right rotated. to check if str2 is left rotation of str1, then use,

strstr(strcat(strrev(str1),strrev(str1)), str2)

- Parikalpa August 24, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

left rotation has nothing different than right rotation.

e.g. ABCDE
right rotate 2: DEABC
left rotate 3: DEABC

right rotate m=left rotate (N-m)

- likexx October 07, 2007 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

perfect...

- Anonymous October 09, 2007 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

execellent solution by "K" .
It doesnt realy matter whether u rotate it to the left or to the right.. the answer is perfect

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

Right and Left does not matter

strstr( strcat(s1,s1),s2); should work in all the cases.

- Amit March 25, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

K solution is the best and simplest one, rest sux...

- Anonymous April 22, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@k...awesome

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

return (strlen(s1) = strlen(s2))? strstr(s1, strcat(s2,s2)):False;

- now September 19, 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