Skip to main content

    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