Searching a Unimodal Function
How to find the peak (or trough) of a function that rises then falls using far fewer evaluations than checking every point — the ternary-search technique, and why it needs unimodality to work.
Prerequisites: Binary Search Patterns
You're handed a function you can only evaluate point by point (no formula, no derivative) and told it's unimodal on an interval — it strictly increases, hits one peak, then strictly decreases, with no other bumps. Where's the peak? Checking every point is wasteful. The right tool is a cousin of binary search, adapted for a peak instead of a sorted target: ternary search.
Why two probe points are enough
Pick two points inside the current interval , evenly spaced so they split it into thirds. Because the function is unimodal, comparing and tells you which third of the interval can be safely thrown away:
- If , the peak cannot lie to the left of — the function is still climbing at (or the peak is between and ), so discard and search .
- If , symmetric reasoning discards and you search .
In plain English: two probes are enough to identify which side is "still going uphill," and unimodality guarantees the peak must be on that side or between the probes — never on the strictly-downhill side you just discarded. Each round shrinks the interval to two-thirds of its previous size, so after rounds the interval is of its original width — shrinking to arbitrary precision in a number of steps that grows only logarithmically with how precise you need to be.
Worked example: finding the peak of on
This is unimodal with true peak at . Round 1: , probes , . , . Since , discard the right third: new interval . Round 2: , probes . , . Now , so discard the left third: new interval . Notice the true peak, , is inside at every step — after just two rounds the search window has already shrunk from width 10 to width 4.44, and it keeps shrinking by a factor of every round from here.
Adjust the curve and imagine dropping two probe points inside the visible window: comparing their heights always tells you which side to discard, because a single-peaked curve can never have its true maximum on the strictly-descending side of either probe.
What this means in practice
Ternary search is the go-to technique whenever an interview question describes a "one hump" relationship — profit that rises then falls as you increase price, accuracy that rises then falls as you tune a single hyperparameter, PnL that rises then falls with position size — and you need the optimal point using few evaluations, especially when each evaluation is expensive (a backtest run, a live query). It generalizes the "cut the search space in half" instinct from binary search to "cut it to two-thirds," using a comparison of function values instead of a comparison against a fixed target.
For a unimodal function on an interval, two evenly spaced probe points let you discard one-third of the interval each round — the side where the function is provably still descending. Repeating this (ternary search) narrows in on the peak in roughly rounds instead of a full linear scan.
Ternary search silently gives a wrong answer if the function isn't actually unimodal — even one extra local bump can hide the true peak in a discarded third. Always confirm (or be explicitly told) that the function has exactly one hump before trusting this technique; a quick plot or a monotonicity check is worth doing first.
Practice in interviews
Further reading
- Cormen et al., Introduction to Algorithms, ch. 2 (binary search generalizations)