How does median-of-medians guarantee worst-case O(n) selection, and why isn't it the default?
answer
- Buy a provably good pivot, not a lucky one
- Groups of five, medians of groups
- Half the medians, three elements each
- About 3n/10 guaranteed on each side
- Recursion fractions must sum below one
basics
~20 sMedian-of-medians splits the data into groups of five, takes each group's median, and recursively selects the median of those medians as the pivot. That pivot is guaranteed to discard about 30 percent of the data, making the worst case linear, but its constant factors are so high that routines prefer random pivots plus a fallback.
solid answer
~50 sThe trick is spending work to buy a pivot with a *guaranteed* split. Divide the range into groups of five, find each group's median in constant time, then recursively select the median of those `n/5` medians. That value beats at least half the group medians, and each of those beats two more elements in its own group, so at least roughly `3n/10` elements are below the pivot and roughly `3n/10` above it. The surviving side is therefore at most about `7n/10`, giving `T(n) <= T(n/5) + T(7n/10) + O(n)`, and because `1/5 + 7/10 = 9/10 < 1` that solves to O(n) in the worst case. It is not the default because the pivot search itself is several extra passes with poor locality; random pivots are dramatically faster in practice. Real routines use introselect: run the fast path, count bad partitions, and switch to the guaranteed scheme only after too many.
go deeper
Know that a worst-case linear selection method exists and that it works by choosing a pivot whose split quality is provable rather than lucky. The proof itself is above this level.
Be able to walk the construction: groups of five, group medians, a recursive call to pick the median of those, and the counting argument that yields about 3n/10 on each side.
Explain the recurrence and why the fractions summing below one is what makes it linear, then justify why you would still ship random pivots with a counted fallback rather than this scheme by default.
Decide where a guarantee is actually worth its constant factor: a hard latency budget or attacker-influenced input may justify the hybrid, while most batch work should not carry a scheme the team must maintain and explain.
## The problem it solves Partition-based selection is linear only in expectation, because a run of extreme pivots reduces the range by one element at a time. Median-of-medians removes the expectation by making pivot quality a *provable property* rather than a probabilistic one. It is the canonical answer to the interview follow-up "can you guarantee linear time?", and its value is largely as a proof, which is why it is more often asked about than deployed. ## The construction 1. Split the current range into groups of five consecutive elements (one group may be short). 2. Find the median of each group directly. Five elements can be sorted with a fixed number of comparisons, so this is constant work per group and linear over the range. 3. Collect the `n/5` group medians and **recursively select** their median. That value is the pivot. 4. Partition around it, then recurse into the single surviving side exactly as ordinary selection does. Step 3 is the part people misremember. It is a recursive call to the same selection routine, not a sort of the medians, and its cost is what appears as `T(n/5)` in the recurrence. ## Why the split is guaranteed Call the chosen pivot `m`. By construction `m` is the median of the group medians, so at least half of the `n/5` group medians are less than or equal to `m`, which is about `n/10` groups. Within each of those groups, the group median is itself greater than or equal to two other members of its group. So each contributes three elements no greater than `m`, and the count of elements known to be at or below `m` is at least about `3n/10`. The symmetric argument gives about `3n/10` at or above it. The consequence is the useful part: neither side can exceed about `7n/10`. The pivot is not the true median and does not need to be; it only needs to keep a fixed fraction off the surviving side, which is precisely the difference between geometric shrink and the arithmetic shrink that produces quadratic behaviour. ## The recurrence `T(n) <= T(n/5) + T(7n/10) + O(n)` The first term is the recursive pivot selection, the second the recursion into the surviving side, the third the grouping and the partition scan. The sum of the recursion fractions is `1/5 + 7/10 = 9/10`, strictly less than one, so the work at successive levels decays geometrically and the total is O(n). If the sum had reached one, the levels would each cost about `n` and the total would be `n log n`. That is exactly why groups of five: with groups of three the guarantee weakens to about `n/3` and `2n/3`, whose fractions sum to one, and the proof collapses. Groups of seven work too and are sometimes used in the analysis; five is simply the smallest odd size that closes the argument. ## Why it stays on the whiteboard The hidden constant is large. Every level pays for grouping, per-group median computation, a full extra recursive selection just to obtain a pivot, and only then the actual partition, with memory access patterns far less friendly than a straight scan. A randomised pivot costs one draw and is overwhelmingly likely to give a decent split, so on real data it wins by a wide margin, often by an order of magnitude. Engineers do not buy the guarantee because the event it insures against is vanishingly rare once the pivot is randomised. What production code does instead is hybridise. Introselect runs the fast randomised path while counting recursion depth or bad partitions; if the count crosses a threshold, meaning the fast path is behaving pathologically, it switches to a guaranteed-good pivot rule for the remainder. That yields fast typical performance with a worst-case bound preserved, which is the same design philosophy as introsort's fallback from quicksort to heapsort. Mainstream standard libraries differ here in ways worth knowing: C++ and Rust both expose a partial-selection routine built on this hybrid idea, while several other widely used runtimes ship no selection primitive at all and leave callers to sort. ## How to present it in an interview State the guarantee before the mechanism: "a pivot that provably discards a constant fraction gives a worst-case linear bound". Then give groups of five, the `3n/10` count, the recurrence, and the observation that the fractions must sum below one. Close with the judgment: you would reach for the hybrid in a routine that must not have a tail, and for plain random pivots everywhere else. A candidate who recites the proof but cannot say why the technique is rarely deployed has learned the exam and not the tradeoff.
- Why groups of five rather than three?With groups of three the guarantee weakens to roughly n/3 discarded, so the recurrence becomes T(n/3) + T(2n/3) + O(n), whose fractions sum to exactly one. Every level then costs about n and the total is n log n, not linear. Five is the smallest odd group size whose fractions sum below one.
- How does a hybrid selection routine decide when to abandon the fast path?It counts a budget, typically recursion depth or the number of partitions that failed to shrink the range enough, scaled to something like a multiple of log n. Crossing the budget is evidence that pivots are behaving pathologically, and the routine switches to a guaranteed-good pivot rule for the remainder rather than restarting.
- Is the median-of-medians pivot the true median of the data?No, and it does not need to be. It is only guaranteed to lie somewhere in the middle 40 percent or so of the ordering. That is enough, because linearity requires a constant fraction discarded per round, not an exact split.
It is like paying a surveyor before digging: the survey costs real time, but it guarantees you never dig on the wrong side of the field, whereas guessing is far faster and almost always right.
saying these in an interview costs you the question
- Sorts the group medians instead of recursing on them
- Claims the pivot is the exact median
- Says it is the fastest selection method in practice
- Cannot say why groups of three fail
- Thinks the guarantee is probabilistic rather than worst-case