LearnThatStack Ace your next interview
Big-O & Complexity Analysis · question
Question 3 of 57

What is the difference between best, average, and worst case complexity?

beginner
← All Big-O & Complexity Analysis questions
Re-explain

Best, average, and worst case describe one algorithm running on different inputs. Worst case is the slowest an input can make it. Best case is the luckiest input. Average case is what you expect over typical data.

Take searching an unsorted list for a value. Best case, it sits first, so O(1). Worst case, it sits last or is missing, so O(n). Average case lands around the middle, still O(n).

You usually plan around worst case because no input can make the algorithm run slower than that. Best case is often useless for guarantees. Average case matters when you know the data behaves normally and rare slow inputs are acceptable in your system.

Rewriting in plainer words…

This answer doesn't lend itself to a diagram - it reads best . No credits were charged.

Why there's no diagram: “”

The interactive diagram is below the answer - jump to diagram ↓ · Below it, the related concept . Jump to it ↓

The diagram below the answer is the concept . Jump to it ↓

Tailored explanation · switch back to · ·
What should the new diagram focus on?
Guided practice for Big-O & Complexity Analysis One question at a time. Answer out loud, get graded, see what you missed.
How well did you know this?
AI:

Saved in this browser - sign in to keep your review list.

How should your speech become text?

Listening… your words appear above as you speak - tap Stop when you're done.

Recording · cr - tap Stop & transcribe when you're done.

Transcribing with AI…

Voice:

Keep going - a few more words and AI can grade it.

Interview lens How interviewers actually use this question

Likely follow-ups

  • Quicksort's worst case is O(n^2). Why is it still a common default sort? Average is O(n log n). Random or median-of-three pivots make O(n^2) rare, and a fallback sort can cap the worst case.
  • Hash table lookup is O(1) on average. What pushes the worst case to O(n)? Collisions. If many keys land in one bucket, a lookup scans them all. Mention the hash function, load factor and resizing.
  • Is best case the same thing as Big-Omega, and worst case the same as Big-O? No. A case picks which input you run, and O, Omega and Theta bound a growth rate. Linear search's worst case is Theta(n).
  • How is average-case complexity different from amortized complexity? Amortized bounds the total cost of any sequence of operations, with no probability. Average case is an expectation over inputs.

What you can say

  1. Best, average, and worst case all describe the same algorithm, just running on different inputs of the same size.
  2. Worst case is the slowest any input can make it, best case is the luckiest input, and average case is what you expect over typical data.
  3. Take searching an unsorted list for a value. If it sits first, that's O(1), and if it sits last or isn't there, that's O(n).
  4. On average the value lands somewhere around the middle, so the average case is still O(n).
  5. I plan around worst case, because no input can make the algorithm slower than that. Best case tells you almost nothing you can rely on.
  6. Average case is the right number when you know your data behaves normally and your system can live with the occasional slow input.

Weak answers to avoid

  • Treats best case as Big-Omega and worst case as Big-O A case picks the input, and O, Omega and Theta bound a function. Show you keep them apart by saying linear search's worst case is Theta(n).
  • Sells the algorithm on its best case Best case guarantees nothing, because a lucky input won't show up under real load. Lead with worst case, then say when average case is good enough.
  • Gives an average case without saying average over what An average needs an assumption about which inputs show up. Name it, for example random data, or the number means little.
  • Calls average-case linear search O(n/2) Big-O drops constants, so n/2 checks is still O(n). Average and worst case can grow at the same rate and differ only by a constant.
Interview lens

Likely follow-ups, what you can say, and the weak answers to avoid.

Sign in free to open it Free account - the lens opens as soon as you're back.

Want a quick review of the fundamentals? See the Big-O & Complexity Analysis cheatsheet.

← Back to all Big-O & Complexity Analysis questions
Pro · $10/mo

50 of 57 Big-O & Complexity Analysis answers are in Pro.

Full answers, code samples, and AI explanations that go simpler or deeper. Cancel anytime.

  • Full answers + code
  • AI explanations, simpler or deeper
  • 1,000 AI credits / month
  • Cancel anytime