Microsoft Interview Question for Software Engineer / Developers


Country: United States
Interview Type: Phone Interview




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

src = "hello"
sch = "lohel"
sch+sch = ""lohellohel"

src would always appear in sch + sch.
O(n) at the cost of O(n) extra space.

Otherwise you could just do a modified KMP algorithm on src and sch
Make sure that during comparisons when you move to the next character in sch, do (ch+1)%strlen(sch)

- kill February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Yeah, or vice versa, do srcsrc, which was my original answer. The follow up question then became, what if my src is huge and I don't want to concatenate it, what can you do -- Which I started thinking of using KMP for a partial match.

- IHE February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

You dont need to concatnate, what Kill has said is that you increase the index in sch in circular way
@Kill i was wondering if we could use the intermediate step of KMP to figure it out right away

- abhishek March 13, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int findRotationIndex(const string & orig, const string & rot){

    unsigned i=0;
    unsigned orig_len = strlen(orig);
    
    if(orig_len != strlen(rot))
        return -1;
        
    while(orig[i]){
        unsigned j=0;
        if(rot[j] == orig[i]){
            
            for(unsigned k=i+1;k<orig_len;++k){
                if(rot[++j] != orig[k]){
                    break;
                }    
            }
            if(k == strlen(orig)) return i;
            else ++i;
        }
        else{
            ++i;
        }
    
    }
    
    return -1;
}

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

So, O(n^2) time, but relatively good with space. I was using a KMP variant to get a partial match, and if the pattern also hits the end of the src, the starting index of the match is the rotation point. That way, I did it in O(m+k), however, I need extra space O(n) for the partial match table for KMP.

- IHE February 18, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What is KMP?

- yingdi1111 February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Knuth-Morris-Prat algorithm. See wikipedia

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

Append sch to itself. Find src in resultant string. The starting index of the src in the resultant string should be the rotation point.

Example:

src = tester
sch = sterte

Append sch to itself.

stertesterte

Find src in above string. Its the index-4 where the rotation point exists.
Let me know if understanding is wrong.

- bachu February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it's perfect, except that:
complexity O(n^2).

- TheGhost February 23, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No, its O(n) for time and an O(n) for space. Searching a substring in a string is an O(n) operation if you use KMP

- sachin March 14, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

What is KMP?

- yingdi1111 February 19, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

it is a string matching algorithm, that can find all the occurrences of a pattern P in text T. and it requires O(n) time and O(n) space.

- hitman February 19, 2012 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>

void removefirstchar(char * str)
{
int i =0;
for(i =0; i < strlen(str); i++)
{
str[i] = str[i+1];
}
}
int main()
{
char str[] = "TestOfTest";
char str1[] = "OfTestTest";
char *pos = NULL;
int length = strlen(str1);
int i =0;
for(i=length ; i>0; i--)
{
pos = strstr(str, str1);
if(i == length && pos)
{
printf("0");
return 0;
}
if(pos)
{
printf("%d\n", i);
return 0;
}
else
{
if(strlen(str1) > 1)
removefirstchar(str1);
}
}
printf("No rotation found");

}

- Santosh Diwase March 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

#include<stdio.h>
#include<string.h>

void removefirstchar(char * str)
{
int i =0;
for(i =0; i < strlen(str); i++)
{
str[i] = str[i+1];
}
}
int main()
{
char str[] = "TestOfTest";
char str1[] = "OfTestTest";
char *pos = NULL;
int length = strlen(str1);
int i =0;
for(i=length ; i>0; i--)
{
pos = strstr(str, str1);
if(i == length && pos)
{
printf("0");
return 0;
}
if(pos)
{
printf("%d\n", i);
return 0;
}
else
{
if(strlen(str1) > 1)
removefirstchar(str1);
}
}
printf("No rotation found");

}

- Santosh Diwase March 17, 2012 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

public void findRotation(string orig, string rotated)
        {
            int rot = 0;
            for (int i = 0; i < orig.Length; i++)
            {
                if (orig == rotated)
                    break;
                orig = orig[orig.Length - 1] + orig.Remove(orig.Length - 1);
                rot++;
            }
            Console.WriteLine(rot);
        }

- lazycoder June 27, 2012 | 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