Microsoft Interview Question for Software Engineer in Tests






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

int FindSortedArrayRotationIterative(int *array, int length)
{
    int start = 0;
	int end = length-1;
	if(array[start] < array [end])
		return start;	
	while(start != end)
	{
		int middle = (start+end)/2;
		if(array[start] < array[middle])
			start = middle;
		else
			end = middle;
	}
	return start+1;

}

The above code was messed up because of indentation.

- Daniel Johnson February 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

I did not understand how it works. Running through the example array in the problem it gives me 1 rotation whereas the result should be 3. Please correct me if I am wrong.

- russoue February 04, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

#
int FindSortedArrayRotationIterative(int *array, int length)
#
{
#
int start = 0;
#
int end = length-1;
#
if(array[start] < array [end])
#
return start;
#
while(start != end)
#
{
#
int middle = (start+end)/2;
#
if(array[start] < array[middle])
#
start = middle;
#
else
#
end = middle;
#
}
#
return start+1;
#

}

But this algorithm only works if the numbers are unique in the array. The example where the numbers are repeated is actually a O(n) at worst.

This questions was for a contraction position. He wanted me to write a recursive function that will scan both the partitions of the array and would work in O(n) time.

- Daniel Johnson February 04, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

wat do u mean by rotation

- Anonymous February 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If you have a sorted array as:

1 2 3 4 5 6 7

Now if you rotate it say 3 times it becomes:

5 6 7 1 2 3 4.

So given this array {5,6,7,1,2,3,4} return the number of times this array has been rotated.

This problem is trivial in O(logN) in unique number case but O(n) for repeated numbers.

- Daniel Johnson February 05, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

i want to know the process of rotating the array

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

We can give a general solution which includes for both unique and repeated numbers:

int findNumberOfRotations( int array[], int n)
{
int i;
for(i=0;i<(n-1);i++)
{
if(array[i]>array[i+1))
return i;
}
return 0;
}

This program will work for both the cases and maximum runtine of this is n. It will be lower than in average cases.
Do you agree with it?

- Shruthi February 05, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

Hi Shruthi,

Your solution surely works. The main idea is to find a recursive solution that would run in O(logN) when given unique numbers and switch to O(n) during repeated numbers.

- Daniel Johnson February 07, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

I think it's impossible, as you can't tell which part contains 2, in a 1111111211111 case unless you do a O(n) scan, however, you could first give a nlogn try and if it cant find then we do a o(n) search

- Anonymous November 03, 2010 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

Not sure about Daniel's answer. The spec didn't mention if it is a ascending or descending list, so we should consider both.
The spec didn't mention this list contains unique elements or not, so the return value could be "don't know".
This is a O(n) question to me.

- Bill Li February 06, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

If this case of ascending and descending, Shrthi's solution also may not work as expected.

- Anonymous April 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

If numbers are descending and then rotated, the condition will be array[i] < array[i+1]. And the return value must be i+1 because i started at 0. Please do check...

- Anonymous April 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

int FindSortedArrayRotationIterative(int *array, int length)
{
    int start = 0;
	int end = length-1;
	if(array[start] < array [end])
		return start;	
	while(start != end)
	{
		int middle = (start+end)/2;
		if(array[start] < array[middle])
			start = middle;
		else if(array[start] > array[middle])
			end = middle;
		else
			start++;
	}
	return start+1;
}

- Hemant Jain February 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

It doesn't Hemant. And you posted the same solution that I did.

Your solution does not account for the case when the two numbers you check are equal. Check for 1 1 1 1 1 1 2 1 1 1.

- Daniel Johnson February 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

its working just one correction. What you did wrong is extra pruning.

#include<iostream>

using namespace std;
int FindSortedArrayRotationIterative(int *array, int length);


int main()
{

int arr[]={1,1,1,1,1,1,2,1,1,1};


cout<<FindSortedArrayRotationIterative(arr,10);
	

}




int FindSortedArrayRotationIterative(int *array, int length)
{
    int start = 0;
	int end = length-1;
	if(array[start] < array [end])
		return start;	
	while(start != end)
	{
		int middle = (start+end)/2;
		if(array[start] < array[middle])
			start = middle;
		else if(array[start] > array[middle])
			end = middle;
		else
			start++;
	}
	return start;
}

- Hemant Jain February 14, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

First of all, It seems that the question was not clear because it was not mentioned whether the list of integers are in sorted order (whether it is ascending or descending), and second is it was not that there are duplicate number(s) in the list... So that we have to assume many cases here to write code.

- Anonymous April 10, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

this sol will take good of both if unequal then will divide solution and if equal then will move linearly after all the sol is O(N)

- Hemant Jain February 12, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

enum rel {gt,lt,eq} relation, lastRelation;
relation = eq;
do
{
if(arr[i]>arr[i+1])
{ lastRelation = relation;
relation = gt;

}
else if(arr[i]<arr[i+1])
{ lastRelation = relation;
relation = lt;
}
else
continue;


if(relation!= lastRelation && lastRelation != eq)
return i-1;
++i;
}while(i<arrSize);

- Deep February 21, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

does divide and conquer help here ? i have implemented it in 'C' . i am not good at recursion. comments are welcome .

#include<stdio.h>
int posit(int[],int,int);
int posit(int in[],int sta,int end)
{

int mid=0;
static int val;
if(sta==end)
{
return val;
}
else
{

mid=(sta+end)/2;
if(in[mid]<in[mid+1] || in[mid]==in[mid+1])
{

posit(in,sta,mid);
posit(in,mid+1,end);

}
else
{
val=mid+1;
}
}

}
void main()
{

int in[]={1,2,1,1,1,1,1};
int i=0,pos=0;
clrscr();
for(i=0;i<7;i++)
printf("%d\t",in[i]);

pos=posit(in,0,6);

printf("\nRotation is %d\n",pos);
getch();

}

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

int FindMax(int array[],int left,int right){

if (left==right){

return left ;
}
int center=(left+right)/2;
int leftMax=FindMax(array,left,center);
int rightMax=FindMax(array,center+1,right);

if (array[leftMax]>array[rightMax]){
return leftMax;
}
else
{
return rightMax;

}



}

int Max(int array[],int n){

return FindMax(array,0,n-1);


}

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

#include<stdio.h>
int find_place(int *a,int p,int r)
{
if(p<r)
{
int x,y,q;
q=(p+r)/2;
x=find_place(a,p,q);
y=find_place(a,q+1,r);
if(x==0&&y==0)
{
if(a[q]<=a[q+1])
return 0;
else
return q+1;
}
else
return (x?x:y);
}
return 0;
}

main()
{
int a[10];
int n,i;
scanf("%d",&n);
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("%d",find_place(a,0,n-1));
}

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

the last three answers are just duplicates of each other. guys don't repost others answer just by changing a couple of lines.

- xin.hu@hotmail.com October 08, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Since the order asked for is less than O(n), the only option i can think of is binary search which would give us O(logn). Here is the pseudo code.
--> Keep doing binary search till u find the max number in the array.
--> On each iteration of binary search, look at left and right immediate numbers of the number u reach, and do the next iteration 'towards the right if left and right are equal or if right > left' And 'towards the left otherwise'.
--> Keep track of the location of the number u reach on each iteration.
--> Stop when the current number is greater than the left and right
--> subtract the location of the max number in the end, with the length of the array to find the solution.

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

Assuming the sorting order is ascending and all the values are unique the following can be a solution:

int findRotation(int *array, int length)
{
        if (!length || 1 == length) return 0;
        int start = 0;
        int end = length - 1;
        if (array[start] < array[end])
                return 0;
        int middle;
        while (start != end)
        {
                middle = (start + end) / 2;
                if (array[start] > array[middle])
                {
                        if (array[middle + 1] > array[end])
                                start = middle + 1;
                        else if (array[middle] > array[middle + 1])
                                return middle + 1;
                        else
                                end = middle;
                }
                else
                        return middle + 1;
        }
}

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

O(n) Solution. Modifying Shruthis solution to account for both ascending and descending sorting.

int findNumberOfRotations( int array[], int n)
{
int i;
if(n==1)return 0;
for(i=1;i<(n-1);i++)
{ 
if((array[i] -array[i-1])*(array[i+1]-array[i]) < 0) 
return i;
}
return 0;
}

- Anonymous March 16, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Here is my code. The problem can be solved recursively.

void FindRotatedElements(int *array,int l_idx,int r_idx,int *rotated_elts)
{
int mid_idx = (l_idx + r_idx)/2;
int num_elts = r_idx - l_idx + 1;

if(num_elts == 1)
return;

if(num_elts == 2)
{
if(array[l_idx] > array[r_idx])
(*rotated_elts)++;
return;
}

if(array[mid_idx + 1] > array[r_idx]) //Right component has rotated elements
{
*rotated_elts += mid_idx + 1; //Everything in left half is rotated
FindRotatedElements(array,mid_idx+1,r_idx,rotated_elts);
}
else //Right is perfectly sorted, left is mixed so find elements on left as long as there is one non-rotated element
{
if(array[mid_idx] > array[mid_idx + 1]) //Left is completely rotated
*rotated_elts += mid_idx + 1;
else
FindRotatedElements(array,l_idx,mid_idx,rotated_elts);
}
}

- San March 09, 2011 | 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