## 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.

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

i guess it gives segmentation fault with strcat

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

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.

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

yours is good one, but,

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

ABCDAB is enough. is that true?

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;

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

That's a really neat solution. Good job!

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

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.

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

DABC is valid. it should return true.

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

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

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

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)

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

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)

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

perfect...

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

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.

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

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

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

@k...awesome

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

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

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.

### 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.