Coding & Algorithms · Interview Question
Expected and worst-case time complexity of quicksort, and why does the expected case dominate in practice?
How to answer
Expected O(n log n); worst case O(n²) when pivots split badly (e.g. sorted input with a naive first-element pivot). With a randomized pivot, repeatedly lopsided splits decay fast, so expected runtime is O(n log n) and large deviations are astronomically unlikely.
Key idea: Calling quicksort 'O(n log n)' flatly without the O(n²) worst case, or forgetting randomization (not the algorithm) makes the bad case negligible.
More: Quant interview prep · Quant salary
Related Coding & Algorithms questions
- A fair six-sided die pays you its face value in dollars. What is the risk-neutral fair price to play this game once, and why?
- Draw a uniformly random element from a stream of unknown length in O(1) memory. How does reservoir sampling work?
- Find for each array element whether its complement (target - x) exists. Naive is O(n²). Get it to O(n)?
- Given a sorted array, how do you find a target in O(log n), and what are the two most common bugs?