Best / Worst / Average Case Analysis

0/12 in this phase0/57 across the roadmap

šŸ“– Concept

For the same algorithm, performance can vary depending on the specific input. We analyze three cases:

Best Case (Ī© — Omega): The minimum operations for any input of size n. Example: searching for an element in an array — best case is O(1) if it's the first element.

Worst Case (O — Big-O): The maximum operations for any input of size n. This is what we usually report. Example: searching for an element that doesn't exist — O(n).

Average Case (Θ — Theta): The expected operations over all possible inputs. Often requires probability analysis. Example: on average, linear search checks n/2 elements — still O(n).

Why worst case dominates:

  • It gives a guarantee — your code will NEVER be slower than this
  • It's what interviewers expect
  • It protects against adversarial inputs (security, production reliability)

When average case matters:

  • Quicksort: O(n²) worst case but O(n log n) average — and it's faster in practice than merge sort
  • Hash tables: O(n) worst case but O(1) average — still universally used
  • Randomized algorithms: expected time is often much better than worst case

šŸ  Real-world analogy: Commute time — Best case: no traffic (15 min). Worst case: accident on highway (90 min). Average case: typical traffic (35 min). You plan meetings based on worst case, but choose your route based on average case.

šŸ’» Code Example

codeTap to expand ā›¶
1// LINEAR SEARCH — All three cases
2function linearSearch(arr, target) {
3 for (let i = 0; i < arr.length; i++) {
4 if (arr[i] === target) return i;
5 }
6 return -1;
7}
8// Best case: O(1) — target is first element
9// Worst case: O(n) — target is last or not present
10// Average: O(n/2) = O(n) — on average, check half the array
11
12// QUICKSORT — Average vs Worst case
13function quickSort(arr) {
14 if (arr.length <= 1) return arr;
15 const pivot = arr[arr.length - 1];
16 const left = [], right = [];
17 for (let i = 0; i < arr.length - 1; i++) {
18 arr[i] < pivot ? left.push(arr[i]) : right.push(arr[i]);
19 }
20 return [...quickSort(left), pivot, ...quickSort(right)];
21}
22// Best/Avg: O(n log n) — balanced partitions
23// Worst: O(n²) — already sorted + last element as pivot!
24
25// BINARY SEARCH — Best vs Worst
26function binarySearch(arr, target) {
27 let lo = 0, hi = arr.length - 1;
28 while (lo <= hi) {
29 const mid = (lo + hi) >>> 1;
30 if (arr[mid] === target) return mid; // Best: O(1) — found at mid
31 arr[mid] < target ? lo = mid + 1 : hi = mid - 1;
32 }
33 return -1;
34}
35// Best: O(1) — target is at middle
36// Worst: O(log n) — target at extremes or absent
37
38// INSERTION SORT — Shows all three cases clearly
39function insertionSort(arr: number[]): number[] {
40 for (let i = 1; i < arr.length; i++) {
41 const key = arr[i];
42 let j = i - 1;
43 while (j >= 0 && arr[j] > key) {
44 arr[j + 1] = arr[j];
45 j--;
46 }
47 arr[j + 1] = key;
48 }
49 return arr;
50}
51// Best: O(n) — already sorted (inner loop never runs)
52// Worst: O(n²) — reverse sorted (inner loop runs max)
53// Avg: O(n²) — random order

šŸ‹ļø Practice Exercise

Practice Problems:

  1. What are the best, worst, and average cases for bubble sort?
  2. Why is quicksort preferred over merge sort despite having O(n²) worst case?
  3. For a hash table with chaining, describe all three cases for lookup
  4. An algorithm is O(n) best case and O(n²) worst case. Is it O(n) overall?
  5. Design an input that triggers worst-case quicksort with last-element pivot
  6. Why does insertion sort perform well on nearly-sorted data?
  7. What randomization technique makes quicksort's worst case unlikely?
  8. Compare best/worst/average for: linear search, binary search, hash lookup
  9. A function has O(1) best case and O(n) worst case. What would you report in an interview?
  10. Explain when you'd choose an O(n log n) worst-case algorithm over an O(n log n) average but O(n²) worst-case one

āš ļø Common Mistakes

  • Reporting only best case to make an algorithm sound fast — interviewers want worst case or at least average case

  • Thinking O(n²) worst case means the algorithm is always slow — quicksort is O(n²) worst case but the fastest sorting algorithm in practice

  • Not understanding that Big-O IS the worst case by definition — saying 'worst case Big-O' is redundant

  • Forgetting that best case of O(1) doesn't make an algorithm fast — best case rarely represents realistic inputs

  • Confusing Theta (Θ) with Big-O — Theta is a tight bound (both upper and lower), Big-O is only upper bound

šŸ’¼ Interview Questions

šŸŽ¤ Mock Interview

Practice a live interview for Best / Worst / Average Case Analysis