skip to content

Why does ternary search cost more function evaluations than binary search on the slope sign?

level: seniorimportance: nice to knowfreq 18%

answer

  1. Count evaluations, not iterations
  2. Ternary keeps two-thirds, not one-third
  3. Two probes per step, effective base 1.5
  4. Slope sign is a monotone predicate
  5. Constant factor, not a complexity class

basics

~20 s

Ternary search keeps two-thirds of the interval per step and spends two probes doing it — about 3.4 evaluations per halving. Bisecting on the slope's sign halves the interval for one or two. Same complexity class, worse constant.

solid answer

~40 s

The name fools people into thinking the interval drops to a third; it drops to two-thirds. So each ternary step buys `log2(1/(2/3))` = about 0.58 bits of precision for two evaluations — roughly 3.4 evaluations per halving of the interval. The alternative uses the fact that a unimodal minimum is exactly the sign change of the slope: the slope is negative before it and positive after, which is a monotone predicate, so plain bisection applies. That halves the interval per step with a single slope probe — one evaluation if the slope is available directly, two if you approximate it from a pair of nearby cost measurements. Both are logarithmic in the range; bisecting on the slope just wins the constant, by about 1.7x to 3.4x.

go deeper

for a junior

Get the shrink factor right: a ternary step keeps two-thirds of the interval, not one-third, and pays two evaluations to do it. That single correction is most of what this question is testing.

for a middle

Explain why a unimodal minimum is a sign change in the slope, which turns the problem back into ordinary bisection on a monotone predicate. Be able to compare probes per halving on both sides.

for a senior

Do the accounting out loud — steps per halving times probes per step — and then say when the more expensive method still wins, such as noisy measurements where a local difference's sign is unreliable.

for a principal

Own the framing that the currency is evaluations, and that an evaluation may be a full experiment run. Decide whether a logarithmic probe search is warranted at all against a coarse sweep, and set the evaluation budget before anyone writes the loop.

## The arithmetic nobody does Count **evaluations of the cost function**, not iterations. Iterations are free; evaluations are what you pay for, especially when one evaluation means running a job at a candidate batch size and timing it. Ternary search: each step keeps `2/3` of the interval and spends `2` evaluations. Steps needed to halve the interval: `ln 2 / ln 1.5` = `0.693 / 0.405` ≈ `1.71`. At two evaluations each, that is about **3.4 evaluations per halving**. Bisecting on the slope sign: each step keeps `1/2` and spends one slope probe. That is **1 evaluation per halving** if the slope is directly available, or **2** if you form it from a finite difference — two cost measurements a small step apart, or on an integer knob the pair `f(k)` and `f(k+1)`. So the ratio is between 1.7x and 3.4x in favour of slope bisection. Both remain `Theta(log(range/precision))` evaluations: this is a **constant factor, not a complexity class**. Anyone who says ternary search is "asymptotically worse" has overstated it, and anyone who says the difference does not matter has never paid for an evaluation that takes twenty minutes. ## Why the slope is a monotone predicate This is the conceptual half of the answer. Strict unimodality with a minimum at `x*` says the curve falls before `x*` and rises after. In slope terms: the slope is negative on `[lo, x*)` and positive on `(x*, hi]`. The predicate "slope >= 0 here" is therefore **false, false, ..., false, true, true, ..., true** across the interval — a single crossover. That is precisely the structure plain binary search consumes, and the crossover point is the minimum. So a unimodal optimisation search is not a different algorithm family at all; it is ordinary bisection applied to the derivative rather than to the values. On a discrete knob the same trick uses the forward difference `f(k+1) - f(k)`, which is negative below the optimum and positive above it under strict unimodality. ## Where the misconception comes from Two confusions compound. The first is the name: "ternary" suggests a three-way cut, so people write `log3` and conclude it beats `log2`. The interval is cut *into* three parts but only one is removed, so the surviving fraction is `2/3` and the effective base is `1.5` — worse per step than binary search, not better. The second is counting steps instead of probes: even at equal shrink rates, two evaluations per step would double the bill. ## The refinement worth naming Golden-section search fixes the wasteful half of ternary search. Place the probes at the golden ratio points rather than the thirds, and after each discard **one of the two probes is still interior to the new interval at exactly the right position**, so it can be reused: one *new* evaluation per step, with the interval shrinking by a factor of about `0.618`. That is `ln 2 / ln(1/0.618)` ≈ `1.44` steps per halving at one new evaluation each — about **1.44 evaluations per halving**, comfortably better than ternary search's 3.4 and competitive with a finite-difference bisection. Knowing this is the usual signal that a candidate has thought about the cost model rather than memorised a template. ## When ternary search is still the right call The accounting is not a verdict against it. Slope bisection needs a slope you can trust. Reasons you may not have one: - The cost is only available as a measurement, and two nearby measurements differ by less than the measurement noise — the finite difference is then dominated by noise and the predicate flips randomly near the optimum. - The domain is coarse: adjacent integer knob values may not be evaluable independently, or `f(k+1)` may be as expensive as any other probe with no locality benefit. - The curve is not smooth, so a local difference reflects a kink rather than the global trend, while two well-separated probes still compare meaningfully. Ternary and golden-section searches only ever compare two *separated* values, which makes them robust to exactly the local wobble that ruins a finite difference. That robustness — not the shrink rate — is the honest argument for using them. ## How to present this in an interview Lead with the correction (`2/3`, not `1/3`), give the per-halving evaluation counts on both sides, name that both are logarithmic so the difference is a constant, then say when you would still choose the more expensive method. The failure mode to avoid is arguing rate without arguing cost per step: an algorithm that halves the interval using ten probes loses to one that keeps two-thirds using one.

  • Roughly how many evaluations does each method need to halve the interval?
    Ternary search: about 1.71 steps to halve (`ln 2 / ln 1.5`) at two evaluations per step, so about 3.4. Bisecting on the slope sign: one step per halving, costing one evaluation with a directly available slope or two with a finite difference. Golden-section search: about 1.44 steps per halving at one *new* evaluation each, because one probe is reused, so about 1.44.
  • How does golden-section search save an evaluation per step?
    By placing the probes at golden-ratio positions instead of the thirds. After a discard, one surviving probe already sits at exactly the correct position for the new, smaller interval, so only one fresh evaluation is needed. The interval shrinks by about 0.618 per step rather than 0.667 — slightly better shrink at half the probe cost.
  • Given the accounting, when would you still pick ternary or golden-section over slope bisection?
    When the slope cannot be trusted. If the cost comes from a noisy measurement, two nearby probes differ by less than the noise and the difference's sign becomes random near the optimum; if the curve has kinks, a local difference reflects the kink rather than the trend. Comparing two well-separated values is robust to both. You trade a constant factor in evaluations for a decision that is actually reliable.

saying these in an interview costs you the question

  • Says ternary search is log base 3 and therefore faster
  • Counts iterations instead of function evaluations
  • Claims the two methods differ in complexity class
  • Cannot state that the surviving interval is two-thirds
  • Misses that a unimodal optimum is the slope's sign change
  • Assumes a finite-difference slope is always available and reliable

context