Skip to content
mlmentorship

Find Minimum in Rotated Sorted Array

Retain a minimum inside an inclusive candidate interval, keep a possible midpoint answer, and handle duplicate ambiguity without false logarithmic guarantees.

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.

ML breadth · closed-book attempt

Find Minimum in Rotated Sorted Array

Explain the mechanism, why it works, when it fails, and one alternative.

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.

08:00recommended time

Closing or reloading clears the scratchpad. Only score, weak rubric dimensions, attempt count, and retry date can be stored locally.

30-second answer map

Retain the minimum by comparing middle with the current right boundary.

Problem trace

Shrink an inclusive minimum interval without discarding a possible midpoint answer

Shrink an inclusive minimum interval without discarding a possible midpoint answerCompare 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.The first midpoint is the minimum:retain itNonempty rotation of distinctincreasing integers. Values andoriginal positions stay fixed whilethe inclusive candidate intervalshrinks.Retained original positions 0 through7Middle 1; right 56071821324354657Middle is below right: discard beyondit, not the middle.Retained original positions 0 through3Middle 7; right 16071821324354657Middle exceeds right: discard throughthe middle.Retained original positions 2 through3Middle 8; right 16071821324354657Middle exceeds right: discard throughthe middle.Retained original positions 3 through3One survivor: position 3, value 16071821324354657Return value one. Returning positionthree is a changed output contract.Wrongly discarding the first lowmidpoint makes this source returnsix.Duplicate control: one, one, one,zero, one. Equal midpoint/right onesdo not locate the hidden zero. Thedistinct-only source incorrectlyreturns one.Duplicate-aware equality removes onlythe right endpoint. If it was aminimum, the equal middle remains awitness; smaller interior values alsoremain.All-equal input may remove one itemper iteration: linear worst-casework. Distinct inputs halvecandidates; both use constantauxiliary state. Empty input has nominimum.
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?
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:

ComparisonWhat it provesUpdate
nums[middle] > nums[right]The rotation drop lies strictly after middleleft = middle + 1
nums[middle] < nums[right]The suffix through right is increasing; middle may be minimumright = middle

The source’s else covers the second row because distinct values make equality impossible inside the loop.

IntervalmiddleCompared valuesNext interval
[0,7]31 < 5[0,3]
[0,3]17 > 1[2,3]
[2,3]28 > 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

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

Shrink an inclusive minimum interval without discarding a possible midpoint answerCompare 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.The first midpoint is the minimum:retain itNonempty rotation of distinctincreasing integers. Values andoriginal positions stay fixed whilethe inclusive candidate intervalshrinks.Retained original positions 0 through7Middle 1; right 56071821324354657Middle is below right: discard beyondit, not the middle.Retained original positions 0 through3Middle 7; right 16071821324354657Middle exceeds right: discard throughthe middle.Retained original positions 2 through3Middle 8; right 16071821324354657Middle exceeds right: discard throughthe middle.Retained original positions 3 through3One survivor: position 3, value 16071821324354657Return value one. Returning positionthree is a changed output contract.Wrongly discarding the first lowmidpoint makes this source returnsix.Duplicate control: one, one, one,zero, one. Equal midpoint/right onesdo not locate the hidden zero. Thedistinct-only source incorrectlyreturns one.Duplicate-aware equality removes onlythe right endpoint. If it was aminimum, the equal middle remains awitness; smaller interior values alsoremain.All-equal input may remove one itemper iteration: linear worst-casework. Distinct inputs halvecandidates; both use constantauxiliary state. Empty input has nominimum.
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

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 comparisons and 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.

  1. 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, comparisons, and auxiliary space remain unchanged.

  2. 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 , with 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.