CMD Guide
HomeDSAFoundations

Best, Worst, and Average Cases

Best, Worst, and Average Cases

Two algorithms can both be "linear search" and yet finish in wildly different amounts of time on the same-sized input. If the item you want happens to sit at the front of the array, you find it on the first comparison. If it sits at the very end (or isn't there at all), you scan every element. Same algorithm, same input size n — different amount of work. The case analysis is how we name and reason about that spread instead of pretending an algorithm has a single fixed cost.

The key idea: fix the input size n, then look across all possible inputs of that size. The number of basic operations varies over that set. The best case is the luckiest input, the worst case is the unluckiest, and the average case is what you'd expect over a realistic distribution of inputs. These are three different questions about the same algorithm — not three different algorithms.

Precise definitions

Let T(x) be the number of basic operations the algorithm performs on a specific input x, and let Sn be the set of all inputs of size n. Then:

Two things people constantly conflate. First, case (which input?) is independent of asymptotic notation (how does cost grow?). You can put a tight Θ, an upper O, or a lower Ω bound on any of the three curves. "Worst case O(n2)" and "average case Θ(n log n)" are both legitimate, and describe different curves. Second, the average case requires an explicit probability model of the inputs. Without stating that assumption, "average" is meaningless — the number depends entirely on what you assume the inputs look like.

Worked example: linear search, operations counted

Search an array of n = 8 elements for a target, counting each comparison as one operation. Scan left to right; stop when found.

Notice the average is not the midpoint between best and worst in any deep sense — it fell out of a specific assumption (uniform position, guaranteed present). Change the model — say the target is absent 90% of the time — and the average slides toward n. That's the whole point: the average case is a claim about your data, not just your code.

Pitfalls and what an interviewer probes

When it matters in practice, and trade-offs

Which case you optimize for depends on the stakes of a slow run:

The engineering move is to match the case to the risk: if a bad input is rare and cheap, optimize the average; if a bad input is catastrophic or attacker-triggerable, pay for a good worst case even at some average-case cost.

Key takeaways

🤖 Don't fully get this? Learn it with Claude

Stuck on Best, Worst, and Average Cases? Open Claude, copy a block below, and it'll teach you this exact concept — visually and interactively.

🎨 Explain it visually

Build the mental picture, not memorization.

I just read a lesson on **Best, Worst, and Average Cases** (DSA) and want to truly understand it. Explain Best, Worst, and Average Cases from first principles using ONE vivid real-world analogy and a visual mental model — draw it as ASCII art or a clear step-by-step diagram — with a concrete example using real numbers. Then ask me one question to check I got the mental picture, and wait for my reply. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🤔 Walk me through it (interactive)

Socratic — adapts to where you're stuck.

Teach me **Best, Worst, and Average Cases** interactively. Ask me ONE guiding question at a time, wait for my answer, and adapt to my confusion — build the idea with me step by step instead of explaining it all at once. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧪 Quiz me & fix my gaps

Active recall exposes what you missed.

Quiz me on **Best, Worst, and Average Cases** with 5 questions, easy to tricky, ONE at a time. Tell me if each answer is right; at the end, explain clearly what I got wrong and why. If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.
🧠 Make it stick

Intuition + hook + flashcards for long-term memory.

Help me remember **Best, Worst, and Average Cases** for the long term: give the one-sentence intuition, a memorable hook/mnemonic, a tiny worked example, and 3 active-recall flashcards (Q -> A). If you're unsure or a claim isn't standard, say so and reason from first principles instead of guessing.

📝 My notes