Microsoft Interview Question for Software Engineer / Developers






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

The problem is not clear. Assuming the problem is something like this,

Given array: 1,2,3,4,5,6 is a sorted array
Now the array is rotated 2 times and it becomes like: 5,6,1,2,3,4

Now we have to find the number of rotations which in this case is 2

Here is the logic,

I) One possible linear solution is very straight forward,
Scan through the array from left to right checking for the following condition,
if(a[i] > a[i+1])
i+1 is the number of rotations

II) Now to get a O(logn), we can do a binary search like this,

int findRotations(int a[], int firstIndex, int secondIndex, int last){

int middle = (firstIndex + secondIndex)/2;

if(a[middle] > a[middle +1] && a[middle] > a[last])
return middle +1;
else if(firstIndex == secondIndex)
return 0;
else if(a[middle] < a[middle +1] && a[middle] < a[last])
return findRotations(a, firstIndex, middle, last);
else
return findRotations(a, middle, secondIndex, last);
}

- Tejas November 09, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

question not clear... pls elaborate

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

Your solution will not provide a correct answer if I did 8 rotations. In 8 rotations you will be getting the same output: 5,6,1,2,3,4. But according to your solution it is 2. How you are going to fig out that?

- particularraj November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@particularraj

The above solution was designed based on the assumption that number of rotations will be between 0 to N. If the number of rotations go beyond N... then it is a cycle and there is no way to determine that. However, one possible output that could be displayed is,

In above example, you could display instead of just 2... print ((2 + NX), where N is the number of elements in the array and X is a number from 0 to some max number. So in the above example output will be (2+6X).

I cannot think of anything else. If any one has a better solution, please reply.

- Tejas November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

@particularraj

The above solution was designed based on the assumption that number of rotations will be between 0 to N. If the number of rotations go beyond N... then it is a cycle and there is no way to determine that. However, one possible output that could be displayed is,

In above example, you could display instead of just 2... print ((2 + NX), where N is the number of elements in the array and X is a number from 0 to some max number. So in the above example output will be (2+6X).

I cannot think of anything else. If any one has a better solution, please reply.

- Tejas November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

hhhmmm...I got you Tejas. Basically we have NO idea if it is rotating above N times. If there is any possible solution to fig out this, then I want to know :)

- particularraj November 10, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if we are only given input and output strings, then there is simply no way you can ever figure out the number of rotation if greater than N (i.e. you can only give the answer < N).

- X November 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

thanks anil for clearing this up.

- particularraj November 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

what is "last" here??

- saurabhroongta2 November 14, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Sorry for late reply.
number of rotations are less than N

- handiaya November 15, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

consider the above binary search based solutions for test case
1,1,1,1,1,1,8,1,1
or
1,1,8,1,1,1,1,1,1

- Bugs November 29, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

if its assumed that the original array is sorted, then an easy way is to find the first element of the rotated array in the original array through binary search. O(logn). if it is found in position k, then rotations = length of array - k .

length can be found in O(1) as size(array) / size(int)

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

i think parneeths soln is correct

- spandan1989 February 19, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

int find_rot(int a[],int n)
{
int i = 0;
if(n>1)
{
while(a[i] < a[i+1])
i++;
}
return (i+1);
}

- NKD July 11, 2010 | 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