Microsoft Interview Question
1,268 Interview Reviews |
Back to all Microsoft Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer at Microsoft:
A rotated sorted array (e.g. 34512), find the rotation count (in this case, 3 means rotated by 3).
See more for this Microsoft Software Development Engineer Interview
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (3)
public int FindRotatedIndex(int[] array)
{
if (array == null)
return -1;
if (array.Length == 1)
return 0;
int l = 0; int u = array.Length - 1; int m;
while (u - l != 1)
{
m = (u + l) / 2;
if (MoveToLeft(array,l, m, u))
u = m;
else
l = m;
}
return array[l] > array[u] ? u : l;
}
private bool MoveToLeft(int[] array, int l, int m, int u)
{
// cover this condition ex 3 1 2, ex 9 1 2 3 4 5 6, determine to move to the left,
// the last part of condition is to prevent cases like ex 3 2 1 for moving to left
bool lowerGreaterThanMidAndUpper = (array[l] > array[m] && array[l] > array[u] && array[u] > array[m]);
// to cover conditions where array is not rotated 1 2 3
bool lowerLessThanMidAndUpper = (array[l] < array[m] && array[l] < array[u]);
return lowerGreaterThanMidAndUpper || lowerLessThanMidAndUpper;
}
Helpful Answer?
Yes |
No
Inappropriate?
private bool MoveToLeft(int[] array, int m, int u)
{
return (array[m] <= array[u]);
}
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up



0 of 0 people found this helpful
by Interview Candidate: