Smallest value in a rotated sorted list

Searching sorted data Mid-level Python 3.13+ Kotlin 2.4+ Reviewed By Best Latest Apps

Pick the fix

The list is [4, 5, 6, 7, 0, 1, 2]. At the first step mid holds 7 and the last value is 2. What should happen?

Options

Choose an answer, then open Show answer below to check it.

Show answer

Answer: A Move lo to mid + 1, because the drop must be right of mid

Right: 7 > 2 means the sequence wraps somewhere after mid.

A middle value above the right end proves the wrap point lies to the right, so the left half can be dropped.

Why not the others?
  • B Move hi to mid, because 7 is large. The minimum cannot be at or left of a value bigger than the right end here.
  • C Return 2, the last value. The smallest value, 0, is before the end.

30-second answer

Compare the middle value with the last value. If the middle is bigger, the drop (and the minimum) is to its right; otherwise the minimum is at mid or to its left. Shrink towards it until lo meets hi. O(log n) time, O(1) space.

Practise saying it · 45 s. No recording, just a timer

Explain the rotated-list minimum search.

Say your answer out loud, then check which of these points you covered:

  • Compare mid with the right end
  • Keep the side with the drop
  • O(log n)

Deep dive

Comparing with the right end avoids a special case for lists that are not rotated at all.

With duplicates allowed, equal mid and right values force a linear step (hi -= 1), so the worst case becomes O(n).

The index of the minimum is also the rotation amount.

Python solution Ran on Python 3.13.5 · mypy --strict
Python 3.13
def rotated_min(vals: list[int]) -> int:
    lo, hi = 0, len(vals) - 1
    while lo < hi:
        mid = (lo + hi) // 2
        if vals[mid] > vals[hi]:
            lo = mid + 1  # the drop is to the right of mid
        else:
            hi = mid
    return vals[lo]

Kotlin Ran on Kotlin 2.4.21 · JDK 21.0.12.1
Kotlin 2.4
fun rotatedMin(vals: List<Int>): Int {
    var lo = 0
    var hi = vals.size - 1
    while (lo < hi) {
        val mid = (lo + hi) ushr 1
        if (vals[mid] > vals[hi]) lo = mid + 1 else hi = mid
    }
    return vals[lo]
}

Common mistakes

  • Comparing with the left end and mishandling an unrotated list.
  • Using hi = mid - 1 and skipping the minimum.

Senior take & follow-ups

Explain why comparing with the right end is enough to decide the side.

Follow-up questions

Think of your answer first, then open the hint.

  1. How do you search for a target in the rotated list?

    Show hint

    Find which half around mid is sorted and check whether the target falls in it.

  2. What if duplicates are allowed?

    Show hint

    When vals[mid] == vals[hi], drop hi by one; worst case O(n).

What each operation costs: Binary Search · Linear Search

From the author

DSA Master - Data & Algorithms

Free on iPhone and iPad: watch 50+ data structures and algorithms step through animations, run the code in the playground, and try the daily challenge.

Suggest a fix if something here is wrong or out of date.