Java Zero to All All questionsPractise free
Interview questions › Java Foundations — Zero to Basics › Efficiency & Big-O — Measuring Speed From Zero

Why do we usually quote the worst case, and what are best/average cases?

Level II · JuniorEfficiency & Big-O — Measuring Speed From Zero
Short answer

Best, average and worst case describe the cost on the luckiest, typical, and unluckiest inputs.

Explanation

We usually quote the worst case because it is the guarantee -- it bounds how bad things can get regardless of input, which matters for reliability and tail latency. Average case is useful too: for example HashMap.get is O(1) on average but O(n) in a pathological worst case (many collisions), which is why the average and worst can differ and both are worth knowing.

Common mistakes
  • Quoting the average case without saying so.
  • Not mentioning when the worst case actually happens.

Can you say this out loud in two minutes? Drill it in a timed mock interview, with flashcards and runnable code for Efficiency & Big-O — Measuring Speed From Zero.

Practise free

More on Efficiency & Big-O — Measuring Speed From Zero