Microsoft · Question 3 · Binary Search
Search in Rotated Sorted Array
Problem
Given a rotated sorted array and a target, find the index of target in the array or return -1.
Approach
Apply modified binary search considering pivot rotation.
Time O(logn)Space O(1)
How it works
- 1Find which half is sorted via nums[left] vs nums[mid].
- 2Narrow into the half that can contain the target.
Sample input / output
Input: 7 4 5 6 7 0 1 2 0 Output: 4
Constraints
1 <= nums.length <= 5000; distinct values; -10^4 <= target <= 10^4.
Solution — read it, then walk it
Time O(logn)Space O(1)Loops 2Branches 4Returns 1source · Java
Scroll sideways for wide charts — the view follows the active step.
- start / end
- statement
- condition
- loop
- returns
- definition
1/20
StartSTART
Interactive control-flow diagram and step-by-step code walkthrough.
Recommended resourcesAffiliate links · may earn us a commission
Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Cloud hosting for side projectsShip the portfolio project that gets you hired — free credits for new accounts.Claim credits