Mid
Detailed answer
Binary Search
DSA & Coding Interviews
How do you search in a rotated sorted array?
Short answer: Modified binary search: check which half is sorted, then decide whether the target lies in that sorted half. Still O(log n).
Interview approach
- Compute mid.
- If left half sorted (nums[lo] ≤ nums[mid]): if target in [lo, mid) go left else right.
- Else right half sorted: if target in (mid, hi] go right else left.
Complexity
Time O(log n), Space O(1).
Edge cases to mention
- No rotation
- Duplicates (harder — may degrade to O(n))
- Single element
Draw a rotated array and mark the sorted half each step while explaining.
Share this Q&A
Share preview image: https://www.toolliyo.com/images/toolliyo-logo.png