Microsoft Interview Question for Software Engineer / Developers






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

Can be done in O(log n).
int find_index(int *a,int start,int end)
{
int index=(start+end)/2;
if(a[start]<a[index]<a[end])
return start;
else if(a[start]>a[index])
return find_index(a,start,index);
else if(a[index]>a[end])
return find_index(a,index,end);

}

But I don't understand the use of 'million' numbers here.
Can somebody explain me what all should we look for in general when questions involving large arrays are given.

- yeah December 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
1
of 1 vote

Did you forget to loop?

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

scan the whole array ,compare with the previous element, if less than, this is the start index, o(n)
2.binary search

- joe December 01, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

any way the array is sorted and weather its rotating clock wise or not the starting position will be 0th location right.

- sadineniramarao December 03, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Joe's answer is right.
Only thing is what if array is not rotated at all or array rotated million time (the lenght of array).

Need to put one more condition:
if go through whole array, and can not find previous element greater than the current then starting point is first element in array.

"million" number is just to make thing little challanging.

- Raj December 04, 2008 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

The condition can be improved. Just check whether first element is larger than the last. If not then the first element is the answer.

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

I think the million number suggest that an O(n) solution is not good enough.

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

Million could have been used to give a hint that O(log n) solution is very important for the problem. log of 1 million will be much smaller than O(n) in this case

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

You guys are missing something. What if the array elements are allowed to repeat?

For instance the array was 0,0,0.....,0, 1 and then rotated by unknown quantity. How do you find out where the rotation starts in O(logn) time?

In case the elements are known to be unique, then an O(logn) solution exists as pointed out by "yeah".

- T March 19, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

Can be done in O(log n).

int findstart(int *a,int beg,int end)
{
int mid=(beg+end)/2;
if(beg==mid)
return end;

else if(a[beg]>a[mid])
return findstart(a,beg,mid);

else if(a[mid]>a[end])
return findstart(a,mid,end);

else if(a[beg]==a[mid] && a[mid]==a[end]) // if all 3 are same for cases like
{ // 9 9 9 1 9
if(findstart(a,beg,mid)==a[beg]) // if the starting point returned is same
return findstart(a,mid,end); // as a[beg] i.e the equal value as '9' in
// the above case move to the other half
else
return findstart(a,beg,mid); // if the other half also returns the same
} // value then array contains only 1 unique
} // element

Sample
Initial - 1 1 3 4 4 6 7
Rotated - 6 7 1 1 3 4 4
b m e

b m e

b m e

'b'e
'm'

Initial - 0 1 6 6 6 6 6
Rotated - 6 6 6 6 0 1 6
b m e
s

b m e

'b'e
'm'

Here in both cases b==m at last step and the value end is the index. It is to be noted that whatever the input is the algorithm takes log(n) time.

The code works for cases like 9 9 9 9 0 0 5 also and 9 9 9 9 0 1 9 or even all elements same value.
But for worst case and best case it takes O(logn). And it does take care of repetiton too.

- VK July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about 9 9 9 9 ... 9 1 9 9 .... 9?

- LOLer July 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

What about 9 9 9 9 ... 9 1 9 9 .... 9?

- LOLer July 01, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

it does work!!
if '1' is mid it returns by normal condition since a[beg]>a[mid]
so assuming in the case u r stating '1' is not the mid element.. which means the '1' has to lie on one of the sides of the mid. so as per the code that part of the array is recursively checked for the index. at the final step it'd return the index.

- VK July 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

No it does not.

You check for a[beg] == a[mid] will return end. There won't be any recursion...

- LOLer July 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Oh, my mistake. I did not see your code properly. I apologize.

- LOLer July 06, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 votes

Rather i apologize... been a shabby piece of code.

- VK July 13, 2009 | Flag
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry i din know how to preserve the white space. so only the confusion.

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

sorry i din know how to preserve the white space. so only the confusion.

- VK July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

sorry i din know how to preserve the white space. so only the confusion.

- VK July 01, 2009 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

ok.. be careful next time...

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

int FindPointOfRotation(int a[], int len)
{
	if(!a || (len < 0))
		return -9999;
	if(len == 1)
		return 0;
	int low = 0; int high = len - 1;
	while(low < high)
	{
		int mid = (low + high) / 2;
		if(a[mid] <= a[high])
		{
			if((mid == 0) || (a[mid - 1] > a[mid]))
				return mid;
			else
				high = mid - 1;
		}
		else
		{
			if(a[mid + 1] < a[mid])
				return mid + 1;
			else
				low = mid + 1;
		}
	}
	return 0;
}

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

Above code doesnt work for

{1, 1, 1, 1, 1, 1, 2, 1}

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

Here is another one:

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 05, 2010 | Flag Reply
Comment hidden because of low score. Click to expand.
0
of 0 vote

O(n) can be reduced in some cases by splitting the array in to (n/2) and check whether the first < (n/2 + 1 th) element and continue in the second part if it is true or do the same in the first part of the array
tis might reduce O(n)!!

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

Modified binary search - log(n)

- Anubhav December 21, 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