For a randomized routing algorithm, how does expected-case complexity differ from average-case?
answer
- Averaged over what, exactly?
- One averages inputs, the other coin flips
- Which survives an unusual traffic day?
- Who gets to choose the bad case?
- Expected holds per input, across repeated runs
basics
~20 sExpected case averages over the algorithm's own random choices, so it holds for every input. Average case averages over an assumed input distribution and collapses if that assumption is wrong. Randomization moves the uncertainty from the data to the coin flips.
solid answer
~40 sIf a request router picks a target uniformly at random, saying "expected `O(1)` work per request" is a claim about the router's own randomness: it holds no matter which requests arrive, because the average is taken over repeated runs on the same input. Saying "average `O(1)`" instead is a claim about the request stream — it holds only while traffic matches the distribution you assumed, and an unusual day falsifies it. The practical difference is who picks the bad case: with average-case reasoning the input picks it and can keep picking it, while with expected-case reasoning your own random draws pick it and nobody sending requests can predict them. Both differ from the worst case, which ranges over all inputs and all draws and assumes nothing.
go deeper
Know that every complexity number answers the question 'averaged over what' — over inputs, over the algorithm's own randomness, or over nothing at all in the worst case. Say which one you mean when you quote a bound.
Explain that expected-case bounds range over the algorithm's random choices and therefore hold for every input, while average-case bounds range over an assumed input distribution. Be able to state the assumption behind any average you quote.
Demonstrate that you validate the distribution an average-case claim rests on against real traffic, and that you treat expected-time bounds as claims about the mean rather than about tail latency your users actually feel.
Own the standard for how bounds are written in your organisation's designs: every average names its distribution, every expected names its source of randomness, so nobody plans against an unstated assumption.
### The three quantifiers Every complexity claim hides a quantifier, and "averaged over what?" is the question that exposes it. For an algorithm A and inputs of size n: - **Worst case** — the maximum cost over all inputs. No randomness, no assumptions: `max over x of cost(A, x)`. - **Average case** — the mean cost over an *assumed distribution D on inputs*: `E over x drawn from D of cost(A, x)`. The algorithm may be entirely deterministic; the randomness is a claim you are making about the data. - **Expected case** — the mean cost over the *algorithm's own random choices*, for each fixed input, usually reported as the worst such mean: `max over x of E over r of cost(A, x, r)`. The randomness belongs to the algorithm, not the data, so the bound holds for **every** input. That last point is the whole distinction. An average-case bound is a conditional promise: it holds while your inputs behave as assumed. An expected-case bound is unconditional over inputs and conditional only on your random source — you supply the randomness yourself, so nobody can take it away from you by sending unusual data. ### The routing example Suppose a request router assigns each arriving request to one of m targets by drawing uniformly at random, and you want to reason about the work per request. If you say **"average O(1)"**, you are asserting something about the request stream: given the mix of requests you expect, the mean work per request is constant. An unusual day — a burst from one tenant, a retry storm, a migration that changes the key mix — falsifies the assumption, and with it the bound. The input chose the bad case. If you say **"expected O(1)"**, you are asserting something about the router's own dice: for any request stream at all, the mean work per request over the router's random choices is constant. The bad case still exists — it is the run where the draws clump — but it is selected by your random source rather than by the traffic, and it differs from run to run on the same input. ### What expected does *not* promise An expected-time bound is a statement about the mean. It caps no individual run. An expected-O(1) operation can occasionally be far more expensive; the guarantee is that such runs are rare, not that they are absent. This is why an expected-case bound and a latency SLO are different objects: percentile targets live in the tail, and the tail is exactly what an expectation averages away. When randomized behaviour matters for latency, the useful follow-up is a **concentration** claim — not just "the mean is O(f(n))" but "the probability of exceeding c times that is smaller than some rapidly shrinking bound in n" — which is a strictly stronger statement than the expectation alone. ### Where randomization actually helps Randomization does not delete a worst case; it relocates it. Before randomization, a specific input triggers the bad behaviour deterministically, every single time, and it will keep doing so on every retry. After randomization, the bad behaviour requires an unlucky sequence of internal choices, so the same input is usually fine, a retry is an independent trial, and no property of the data can force the bad path repeatedly. "Unlikely and non-repeatable" is a much better operational position than "guaranteed whenever this shape of input arrives", which is why randomized pivots, random probe orders and random tie-breaking are worth their cost. It is worth separating two families here, because they trade different things away. A **Las Vegas** algorithm is always correct and its *running time* is the random variable — a randomized pivot choice is the classic example. A **Monte Carlo** algorithm has bounded running time and its *answer* carries a small probability of being wrong. Expected-time analysis is the natural language for the first family; probability-of-error analysis is the language for the second. ### How to state each one so it survives review A defensible bound names its quantifier. "Expected O(1) per assignment, over the router's own random draws, for any request stream" is checkable. "Average O(1)" is only checkable once you add "under a request mix with these properties", and it needs monitoring, because distributions drift while code does not. And "worst case O(m)" is checkable by construction — it assumes nothing, which is why it is the number you plan capacity against even when you expect never to see it. ### The one-line test Ask of any average: **over what?** If the answer is "over inputs", you owe the reader a distribution and a way to notice when it stops holding. If the answer is "over the algorithm's coin flips", you owe the reader nothing about the data — but you also cannot promise anything about a single run.
- Can an expected-O(1) routine still be slow on one particular request?Yes. Expected means averaged over the algorithm's random choices, so a single run can land on an unlucky sequence and cost far more. The guarantee is about the mean, and the tail still exists — which is why latency work is stated in percentiles. An expected-case bound on its own says nothing about p99.
- How would you check an average-case claim before shipping it?Name the input distribution it assumes, then test whether production traffic matches it: key skew, batch sizes, arrival patterns. If you cannot state the distribution, the claim is not an average case, it is a guess. And if the distribution can drift, the claim needs a monitor rather than a footnote.
- Does randomization remove the worst case?No. The worst case still exists — it is the run where nearly every random choice goes badly. Randomization changes who controls it: instead of a specific input triggering it deterministically on every attempt, it becomes an unlikely event with a bounded probability that differs from run to run on identical data.
Average case is a weather forecast for your data. Expected case is a die you roll yourself — you can be handed a wrong forecast, but not a loaded die.
saying these in an interview costs you the question
- Uses expected and average interchangeably
- Claims randomization removes the worst case entirely
- Cannot say what the average is averaged over
- Thinks an expected bound holds only for typical inputs
- Assumes an expected-time bound caps every individual run