Best / Worst / Average Case Analysis
š 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
1// LINEAR SEARCH ā All three cases2function 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 element9// Worst case: O(n) ā target is last or not present10// Average: O(n/2) = O(n) ā on average, check half the array1112// QUICKSORT ā Average vs Worst case13function 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 partitions23// Worst: O(n²) ā already sorted + last element as pivot!2425// BINARY SEARCH ā Best vs Worst26function 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 mid31 arr[mid] < target ? lo = mid + 1 : hi = mid - 1;32 }33 return -1;34}35// Best: O(1) ā target is at middle36// Worst: O(log n) ā target at extremes or absent3738// INSERTION SORT ā Shows all three cases clearly39function 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:
- What are the best, worst, and average cases for bubble sort?
- Why is quicksort preferred over merge sort despite having O(n²) worst case?
- For a hash table with chaining, describe all three cases for lookup
- An algorithm is O(n) best case and O(n²) worst case. Is it O(n) overall?
- Design an input that triggers worst-case quicksort with last-element pivot
- Why does insertion sort perform well on nearly-sorted data?
- What randomization technique makes quicksort's worst case unlikely?
- Compare best/worst/average for: linear search, binary search, hash lookup
- A function has O(1) best case and O(n) worst case. What would you report in an interview?
- 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