Common Heaps Pattern
The content discusses two key heap patterns commonly encountered in coding interviews: the Top-K pattern and the K-way merge pattern. The Top-K pattern efficiently identifies the K largest or smallest elements from a dataset using a min-heap or max-heap, achieving a time complexity of O(n log k). The K-way merge pattern merges K sorted sequences into a single sorted sequence using a min-heap, also with a time complexity of O(n log k). Both patterns emphasize the importance of recognizing the appropriate context for their application, distinguishing between unsorted collections and sorted sequences.
Recognizing a heap as the right data structure is only the first step. The next is knowing which pattern to apply. This lesson covers the two most important heap patterns in coding interviews: the Top-K pattern and the K-way merge pattern. They represent two fundamentally different uses of a heap, and together they cover the majority of heap-based interview problems.
Top-K pattern
The Top-K pattern uses a heap of fixed size
For the K smallest elements, we maintain a max-heap of size K and pop whenever the heap exceeds K, discarding the largest seen so far. The key insight is that we never need to sort the full dataset. We only maintain a small heap of size K throughout the traversal.
Sorting the entire input and slicing the last K elements gives the right answer, but costs
When to reach for it
The signal is any problem that asks for K elements from a larger collection without requiring the full collection to be sorted. The collection may be static or arrive as a stream. If the problem involves finding the Kth largest or smallest element, or returning the top K elements by some criteria, this is the pattern.
Pattern recognition: Keywords to watch for are top K, K largest, K smallest, Kth largest, Kth smallest, K most frequent, and K closest. Any problem that asks us to rank or filter a collection down to K elements without full sorting is a Top-K problem.
Time and space complexity
Time
...