Retain a minimum inside an inclusive candidate interval, keep a possible midpoint answer, and handle duplicate ambiguity without false logarithmic guarantees.
QuestionInterview executionCoding practice
Published ·14 min read·Core·Foundation
ML breadth · active recall
Practice before you read
8 minutes. Explain the mechanism, why it works, when it fails, and one alternative.
Retain the minimum by comparing middle with the current right boundary.
Problem trace
Shrink an inclusive minimum interval without discarding a possible midpoint answer
Read it this way:Compare the midpoint with the retained right endpoint. A larger midpoint excludes its prefix; a smaller one may be the minimum, so keep it. The first midpoint here is already the answer. Return its value, not the rotation index. Distinct inputs shrink logarithmically. Duplicates make equality ambiguous: discard only one equal endpoint, preserving a witness. All-equal input can require linear work. Do not modify the source array. When midpoint and right values are equal, can the entire half be discarded?
Sources and provenance
Full interview answer 14 min read
Find the smallest value in a distinct sorted array whose starting position may have been rotated.
Problem
Input. A nonempty integer list nums obtained by rotating a strictly increasing list at some position.
Output. Return the smallest value, not its index or the amount of rotation.
Rules. Rotation moves a prefix to the end without changing the relative order within either part.
Zero rotation and singleton input are valid. Values must be distinct for this reference.
Do not modify the list. Empty input is outside the contract; duplicates are a changed requirement below.
Example.[3, 4, 5, 1, 2] returns 1.
The unrotated list [1, 2, 3] also returns 1.
Picture
Use [6,7,8,1,2,3,4,5]. The minimum’s position remains in inclusive [left,right].
Compare nums[middle] with nums[right] while at least two candidates remain:
Comparison
What it proves
Update
nums[middle] > nums[right]
The rotation drop lies strictly after middle
left = middle + 1
nums[middle] < nums[right]
The suffix through right is increasing; middle may be minimum
right = middle
The source’s else covers the second row because distinct values make equality impossible inside the loop.
Interval
middle
Compared values
Next interval
[0,7]
3
1 < 5
[0,3]
[0,3]
1
7 > 1
[2,3]
[2,3]
2
8 > 1
[3,3]
Return nums[3] = 1. The first midpoint was already the answer, so replacing right = middle with right = middle - 1 fails.
This reuses candidate intervals, but retains a minimum position rather than a target match.
Trace L, m, and R mean left, middle, and right.
Problem trace
Shrink an inclusive minimum interval without discarding a possible midpoint answer
Input and goalFind the smallest value in a sorted array that was rotated once.
Keep the midpointleft=0, right=7, middle=3. The comparison 1 < 5 permits a midpoint minimum, so set right=3.
L607182m13243546R57
interval[0, 7]comparison1 <= 5decisionright = 3
L
0
m
3
R
7
Discard the left partNow left=0, right=3, middle=1. Since 7 > 1, the drop follows middle, so set left=2.
L60m7182R1324354657
interval[0, 3]comparison7 > 1decisionleft = 2
m
3→1
R
7→3
interval
[0, 7]→[0, 3]
Discard midpoint 2Now left=2, right=3, middle=2. Since 8 > 1, index 2 cannot be minimum, so set left=3.
6071L=m82R1324354657
interval[2, 3]comparison8 > 1decisionleft = 3
L
1→2
L
0→not shown
interval
[0, 3]→[2, 3]
Return the converged valueleft and right meet at index 3. The invariant leaves nums[3]=1 as the only possible minimum.
607182min1324354657
interval[3, 3]result1
min
2→3
R
3→not shown
interval
[2, 3]→[3, 3]
Recognize it
Distinct values form two increasing runs after one rotation, and the task asks for the smallest value rather than a particular target.
Keep true
The minimum is always inside inclusive [left, right]. If nums[middle] exceeds nums[right], middle is on the high run; otherwise middle may be the minimum and must be retained.
Reuse it
Pivot searches depend on choosing an endpoint reference and preserving the candidate when equality or ordering does not prove it impossible; this same boundary discipline applies to first-occurrence searches.
left=0, right=7, middle=3. The comparison 1 < 5 permits a midpoint minimum, so set right=3. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why must the first update use right=middle rather than middle-1?
Reference drawing
Problem trace
Shrink an inclusive minimum interval without discarding a possible midpoint answer
Execution trace
Input and goalFind the smallest value in a sorted array that was rotated once.
Keep the midpointleft=0, right=7, middle=3. The comparison 1 < 5 permits a midpoint minimum, so set right=3.
L607182m13243546R57
interval[0, 7]comparison1 <= 5decisionright = 3
L
0
m
3
R
7
Discard the left partNow left=0, right=3, middle=1. Since 7 > 1, the drop follows middle, so set left=2.
L60m7182R1324354657
interval[0, 3]comparison7 > 1decisionleft = 2
m
3→1
R
7→3
interval
[0, 7]→[0, 3]
Discard midpoint 2Now left=2, right=3, middle=2. Since 8 > 1, index 2 cannot be minimum, so set left=3.
6071L=m82R1324354657
interval[2, 3]comparison8 > 1decisionleft = 3
L
1→2
L
0→not shown
interval
[0, 3]→[2, 3]
Return the converged valueleft and right meet at index 3. The invariant leaves nums[3]=1 as the only possible minimum.
607182min1324354657
interval[3, 3]result1
min
2→3
R
3→not shown
interval
[2, 3]→[3, 3]
Recognize it
Distinct values form two increasing runs after one rotation, and the task asks for the smallest value rather than a particular target.
Keep true
The minimum is always inside inclusive [left, right]. If nums[middle] exceeds nums[right], middle is on the high run; otherwise middle may be the minimum and must be retained.
Reuse it
Pivot searches depend on choosing an endpoint reference and preserving the candidate when equality or ordering does not prove it impossible; this same boundary discipline applies to first-occurrence searches.
left=0, right=7, middle=3. The comparison 1 < 5 permits a midpoint minimum, so set right=3. Step through the frames to watch the state change. The last frame shows the answer or the stopping condition.
Check: Why must the first update use right=middle rather than middle-1?
Read it this way:Compare the midpoint with the retained right endpoint. A larger midpoint excludes its prefix; a smaller one may be the minimum, so keep it. The first midpoint here is already the answer. Return its value, not the rotation index. Distinct inputs shrink logarithmically. Duplicates make equality ambiguous: discard only one equal endpoint, preserving a witness. All-equal input can require linear work. Do not modify the source array. When midpoint and right values are equal, can the entire half be discarded?
Code
def find_min_rotated(nums: list[int]) -> int: left, right = 0, len(nums) - 1 while left < right: middle = (left + right) // 2 if nums[middle] > nums[right]: left = middle + 1 else: right = middle return nums[left]
Both branches shrink the interval while preserving the answer. Distinct input needs O(logn) comparisons and O(1) auxiliary space.
An empty array has no minimum. The reference raises IndexError on it.
With duplicates, [1,1,1,0,1] incorrectly returns one because equality does not locate the drop.
Change the Result or Distinctness
Start from find_min_rotated. Two adjustable parts are the returned evidence and the comparison’s equality case.
Return the rotation boundary.
New task: Find the minimum’s index rather than its value.
Picture and change: Keep the distinct-array interval and both updates. Replace the final lookup with return left.
Code: Complete replacement with an explicit nonempty check:
def rotation_index(nums): if not nums: raise ValueError("nonempty input required") left, right = 0, len(nums) - 1 while left < right: middle = (left + right) // 2 if nums[middle] > nums[right]: left = middle + 1 else: right = middle return left
Check: The pictured input returns three. [1,2,3] returns zero because its minimum already starts the array.
Use when: The caller needs a boundary position. Distinctness, O(logn) comparisons, and O(1) auxiliary space remain unchanged.
Permit duplicate values.
New task: Find a minimum in a nonempty rotation of a nondecreasing array.
Picture and change: Separate equality from strict comparison. On equality, discard only right; middle retains an equal witness.
Code: Complete replacement:
def min_with_duplicates(nums): if not nums: raise ValueError("nonempty input required") left, right = 0, len(nums) - 1 while left < right: middle = (left + right) // 2 if nums[middle] > nums[right]: left = middle + 1 elif nums[middle] < nums[right]: right = middle else: right -= 1 return nums[left]
Check:[1,1,1,0,1] returns zero. At its first equality, removing only the last one preserves the hidden zero.
All-equal input can remove only one candidate per iteration. Worst-case work becomes O(n), with O(1) auxiliary space.
Use when: Equality obscures the rotation drop. A surviving minimum is not necessarily a unique rotation boundary.
Rebuild the Discard
Why is discarding one equal endpoint safe, while discarding its entire half can fail?
Answer: If right was a minimum, the equal middle remains one. Equality says nothing about smaller values between them.