Best Average Worst Case Complexity | Java Fresher Interview
Fast recall
Quick revision
Core answer
Whether you can separate the three cases and know why interviews emphasize worst case
Quoting best case to make an algorithm look good, or confusing average with best
Give quicksort: O(n log n) average but O(n^2) worst case, and why we still use it
Interview-ready guide
Interview answer
These three describe how an algorithm behaves on different kinds of input.
The best case is the luckiest input, where the algorithm does the fewest operations. The worst case is the most adversarial input, where it does the most. The average case is the expected cost over typical, realistic inputs.
A clean example is linear search. In the best case the element I'm looking for is the very first one, so it's O of one. In the worst case the element is last, or not present at all, so I scan everything, which is O of n.
The average case, if the element is somewhere in the middle, is about n over two, which is still O of n. When I give a Big-O in an interview, I default to the worst case, because that's the guarantee that holds no matter what input shows up, and in production I have to survive the bad day, not just the lucky one. But the average case genuinely matters too.
The best example is quicksort. On average it's O of n log n, but its worst case is O of n squared, which happens on already-sorted input if you pick a naive pivot. We still use quicksort everywhere because that worst case is rare with good pivot selection and its constant factors are small.
So my point is that quoting only the best case is misleading, and understanding all three is what tells me whether an algorithm is safe or risky for a given workload.
Best Average Worst Case Complexity | Java Fresher Interview
Direct Answer
Best case is the fewest operations for a lucky input, worst case the most for the nastiest input, and average case the expected operations over typical inputs. Big-O usually quotes the worst case because that's the guarantee you can rely on. Example: linear search is O(1) best (first element), O(n) worst (last/absent), O(n) average.
Code Example
int linearSearch(int[] a, int target) {
for (int i = 0; i < a.length; i++)
if (a[i] == target) return i; // best: hit at i=0 (1 step)
return -1; // worst: absent (n steps)
}
// Best case: target == a[0] -> O(1)
// Worst case: target absent / last -> O(n)
// Average case: target somewhere mid -> ~n/2 = O(n)
// Quicksort: average O(n log n), worst O(n^2) on a bad pivot choice
Common Mistakes & Pitfalls
- Quoting best case: saying 'binary search is O(1)' (the best case) hides the real O(log n) guarantee.
- Average == best: average is the expected cost, not the luckiest one.
- Ignoring worst case for quicksort: forgetting it can hit O(n^2) on sorted input with naive pivots.
- Assuming worst case always happens: it's the upper bound, not the typical case.
Check your understanding
Answer these without looking back at the article.
- Why does quicksort degrade on sorted arrays?
- What is amortized complexity?
- When would you prefer merge sort over quicksort?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.