Interview patterns

Heap Top K Pattern Explained: Keep the k Best, Drop the Rest

Updated Nov 28, 2026 · 3 min read

To find the k largest items, keep a min-heap that never holds more than k numbers. Push each number, and whenever the heap grows past k, pop the smallest. At the end the heap holds the k largest, and its top is the kth largest, in O(n log k) time and O(k) space.

Heap / Top K reel cover

Key points

  • ●Clues: k largest, k smallest, kth largest, k most frequent, k closest, top k in a stream, merge k sorted lists.
  • ●For the k largest, use a min-heap of size k and pop the smallest on overflow.
  • ●nums = [3, 2, 1, 5, 6, 4], k = 2: the heap ends as [5, 6] and heap[0] = 5.
  • ●O(n log k) time and O(k) space, versus O(n log n) to sort everything.
  • ●Python's heapq is a min-heap; push negative numbers for a max-heap.
Step-by-step visual sheet: Heap / Top K
The full visual sheet, made with Edithly.Open the interactive sheet

The problem: top 2 of a million

You have a million numbers and you want the 2 biggest. The obvious move is to sort them all and read off the end. That works, but it sorts 999,998 numbers you'll never look at, and it costs O(n log n).

The heap top K pattern does less work, and you'll see it in a lot of interview problems. It keeps only the k best items seen so far, and drops anything that falls out of the top k.

How to spot it

The visual sheet above lists the clues. Reach for a heap when a problem says:

  • k largest, k smallest, kth largest (ranking)
  • k most frequent (frequency)
  • k closest (distance)
  • a stream where data keeps arriving and you keep a running top k
  • merging k sorted lists

The idea: a small min-heap of size k

To keep the k largest, use a min-heap. Push every number in. Whenever the heap holds more than k items, pop the smallest. The smallest is exactly the one that no longer makes the top k, and a min-heap keeps it right on top, ready to remove.

When you've seen every number, the heap holds the k largest, and its top, heap[0], is the kth largest.

Trace it: nums = [3, 2, 1, 5, 6, 4], k = 2

Push Heap after push Over k? Heap after
3 [3] no [3]
2 [2, 3] no [2, 3]
1 [1, 2, 3] pop 1 [2, 3]
5 [2, 3, 5] pop 2 [3, 5]
6 [3, 5, 6] pop 3 [5, 6]
4 [4, 5, 6] pop 4 [5, 6]

The heap ends as [5, 6], and heap[0] is 5, the 2nd largest.

Same list with k = 3: the heap ends as [4, 5, 6], so the answer is 4.

The template in Python

import heapq

def kth_largest(nums, k):
    heap = []                    # min-heap of the k biggest so far
    for x in nums:
        heapq.heappush(heap, x)
        if len(heap) > k:
            heapq.heappop(heap)  # drop the smallest
    return heap[0]

kth_largest([3, 2, 1, 5, 6, 4], 2) returns 5, and with k = 3 it returns 4.

Complexity

  • Time: O(n log k). Every push and pop works on a heap of at most k + 1 items.
  • Space: O(k), just the heap.

Sorting the whole list is O(n log n). When k is 2 and n is a million, log k is tiny next to log n.

Gotchas

  • heapq is a min-heap. For the k largest that's what you want. For a max-heap, push -x and flip the sign back when you read it.
  • Pick the right direction. k largest means a min-heap that evicts the smallest. k smallest flips it: a max-heap (negated values) that evicts the largest.
  • Return the top, not the heap. For the kth largest the answer is heap[0]. For the top k items, return the whole heap.

Practice problems

  • 215 Kth Largest Element in an Array: the template above.
  • 347 Top K Frequent Elements: count first, then keep the k highest counts.
  • 703 Kth Largest Element in a Stream: keep the heap between calls.
  • 973 K Closest Points to Origin: rank points by distance instead of value.
  • 23 Merge k Sorted Lists: the heap holds the front item of each list.

Frequently asked questions

Why use a min-heap to find the k largest elements?

Because the item you want to throw away is the smallest of the k you're keeping. A min-heap keeps that item on top, so popping it is one cheap step. The heap's top at the end is the kth largest.

What is the time complexity of the heap top K pattern?

O(n log k) time, because each of the n numbers is pushed into a heap of at most k + 1 items, and O(k) space for the heap. Sorting everything costs O(n log n).

How do I make a max-heap in Python?

Python's heapq only provides a min-heap. Push -x instead of x, and negate the value again when you pop it.

Heap or sort for top k?

When k is small next to n, the heap wins: O(n log k) beats O(n log n), and it only needs room for k items, which also makes it work on a stream. When k is close to n, sorting is simpler and about as fast.

Turn any problem or document into a visual sheet like this one

Paste a LeetCode problem, a PDF or a link into Edithly and get a step-by-step sheet you can study, share or quiz yourself on.

Make this sheet in Edithly, free