Microsoft Interview Question
1,272 Interview Reviews |
Back to all Microsoft Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Development Engineer Intern at Microsoft:
If a sorted array is rotated, how to find how many times it has been rotated.
See more for this Microsoft Software Development Engineer Intern Interview
Helpful Question?
Yes |
No
Inappropriate?



0 of 0 people found this helpful
by Praveen Chettypally:
1- comparing 1st two elements you can probably say if it's ascending or descending. Or you could ask your interviewer.
2. Once you know the sorting order you can use below logic
public int HowManyRotation(int input)
{
countRotation = 0;
for(int i = 0; i < arraylength - 1 ; i++)
{
if(a[i] < a[i+1] ) ==> ascending order. reverse this for desceding order
{
countRotation = countRotation + 1;
}
if(a[i] > a[i + 1] ==> id change in sorting. reverse this for descending order.
{
countRotation = countRoration + 1;
break;
}
}
}
}
return countRotation;
}