CodeArchon

mediumBinary SearchAmazonMicrosoft

Rotated Low Point

A once-sorted shelf of distinct IDs was rotated by an unknown offset. Find the smallest ID in O(log n).

Given a rotated sorted array of distinct integers `nums`, return its minimum.

Choose approach

3
0
lo
4
1
5
2
1
3
2
4
hi
1/4
Narrationstep 1 of 4

Rotated sorted array: the minimum is the only element smaller than its predecessor. Compare mid to hi to decide which side holds it.

1def rotated_low_point(nums: list[int]) -> int:
2 lo, hi = 0, len(nums) - 1
3 while lo < hi:
4 mid = (lo + hi) // 2
5 if nums[mid] > nums[hi]:
6 lo = mid + 1
7 else:
8 hi = mid
9 return nums[lo]
Variables
lo=0
hi=4

How to think about it

  1. Pick a stable reference: nums[hi] is always ≤ nums[0..inflection].
  2. nums[mid] > nums[hi] → mid is on the left (bigger) run; min is right of mid.
  3. Otherwise mid is on the right (smaller) run — min could be mid itself.
  4. Stop when the interval collapses.

Edge cases to catch in interview

  • Already sorted (zero rotation) — mid ≤ hi branch always wins → returns nums[0].
  • Single element — returned directly.
  • Rotation = n/2 — binary search still converges in log n.

Examples

Example 11
{"nums":[3,4,5,1,2],"values":[3,4,5,1,2]}
Example 20
{"nums":[4,5,6,7,0,1,2],"values":[4,5,6,7,0,1,2]}
Example 311
{"nums":[11,13,15,17],"values":[11,13,15,17]}

Try your own input

SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed