Top-K Problems in Coding Interviews: Heaps, Quickselect, and Bucket Sort Compared
4 views
"Find the k largest elements," "return the k most frequent words," "find the k closest points to the origin." Top-K questions appear constantly in coding interviews because they have several valid solutions with different trade-offs, and interviewers want to hear you compare them. If you can explain three approaches and when to use each, you will handle almost any variant.
Approach 1: Sort everything
Sort the array and take the first or last k items. It is O(n log n), easy to get right, and a perfectly good opening answer. Say it first, then say why you want something better: you are doing more work than needed because you only care about k items.
Approach 2: A heap of size k
To find the k largest values, keep a min-heap of size k. Push each element; when the heap grows past k, pop the smallest. At the end the heap holds the k largest. Time is O(n log k), space O(k). This is usually the answer interviewers expect, and it has a big practical advantage: it works on streams. If the data arrives one item at a time or does not fit in memory, the heap still works.
The most common bug is picking the wrong heap type. For the k largest, use a min-heap (so the smallest of your candidates is easy to evict). For the k smallest or k closest, use a max-heap. Say that reasoning aloud.
Approach 3: Quickselect
Quickselect partitions the array around a pivot like quicksort, but only recurses into the side that contains the k-th position. Average time is O(n), worst case O(n^2) with bad pivots; a random pivot makes the worst case very unlikely. It modifies the array in place and does not return results in sorted order, so mention both limitations. It is a strong choice when the whole array is in memory and you need the best average speed.
Approach 4: Bucket sort for frequencies
For "top k frequent elements," count frequencies with a hash map, then create buckets indexed by frequency (from 0 to n). Walk the buckets from highest to lowest until you collect k items. This runs in O(n) time and avoids comparisons entirely. It only works because frequencies are bounded by n, which is a nice point to make explicitly.
Handling ties and custom ordering
Variants like "top k frequent words" require ties broken alphabetically. With a heap, define the comparison carefully: the element to evict first should be the lowest frequency, and among equal frequencies, the alphabetically largest word. Write the comparator before the loop and test it on a tiny example with a tie.
How to present it in the interview
1. Clarify: is k always valid, can there be duplicates, does output order matter, is the input a stream?
2. State the sorting solution and its cost.
3. Offer the heap solution, explain the heap type, and code it.
4. Mention quickselect or bucket sort as follow-ups, with their trade-offs.
5. Test with k = 1, k = n, and duplicate values.
Practice set
Kth Largest Element in an Array, Top K Frequent Elements, Top K Frequent Words, K Closest Points to Origin, Kth Largest Element in a Stream, and Find K Pairs with Smallest Sums.
Keeping your structure under pressure
Comparing approaches out loud is what separates a strong answer from a correct one. If your thinking gets scattered once a real interviewer is watching, Linkjob AI (https://linkjob.ai) offers real-time help during job interviews and technical interviews, helping you keep the clarify-compare-code-test flow on track.