Data Structures & Algorithms · Interview question

When should you not sort at all?

A strong answer

When you need less than a total ordering. Sorting is O(n log n) (or O(n²)), but many questions have cheaper answers: the minimum or maximum is a single O(n) pass, no sort needed; the k largest/smallest can be had with a partial selection or a size-k heap in O(n log k), far cheaper than sorting all n; the median (or any order statistic) is O(n) average via quickselect; checking existence is O(n) linear scan or O(1) with a hash set. Sorting is only the right tool when you genuinely need everything in order, most commonly to enable repeated binary searches on stable data, where the one-time O(n log n) sort amortizes across many O(log n) lookups. If you'd sort just to find one element or answer one query, you've done O(n log n) work for an O(n) (or better) problem. The discipline is to ask "do I need total order, or just a specific element / partial order?" before reaching for a sort.

What a weak answer sounds like

You know the answer. Do you know what gets you dinged?

Pro breaks down the answer most candidates actually give to this question — and the specific reason an interviewer marks it down. It’s the difference between sounding correct and sounding senior, on all 472 questions.

From the lesson

Sorting Basics

The O(n²) simple sorts vs the O(n log n) fast ones, and why on embedded the simple, in-place insertion sort often beats the asymptotically-better choice at small n.

More Sorting Basics questions

Browse all 472 interview questions