Heap Top-K Interview Pattern

Heap Top-K Interview Pattern

Heap top-k interview problems show up as “k largest,” “k most frequent,” “k closest,” or streaming “always return the median/top.” The pattern is less about memorizing heapq API and more about choosing min-heap of size k vs sorting vs Quickselect—and saying why.

The default top-k move

For “k largest in an unsorted array” when k ≪ n:

Talk track:

“I’ll maintain a min-heap of size k so the root is the threshold for being in the top k. Linear pass, O(n log k).”

For k smallest, symmetric max-heap of size k.

When to pick which tool

SituationPrefer
k largest/smallest, k smallSize-k heap
Need fully sorted outputSort O(n log n) — say if n is tiny
Average O(n) selectionQuickselect — mention, implement only if comfortable
k most frequentCount map + heap on frequencies
Streaming / onlineHeap (or two heaps for median)
k closest pointsMax-heap by distance of size k, or min-heap of all if n small

Interviewers often accept sort for partial credit; the heap shows you care about log k.

Frequency top-k skeleton

count ← frequency map of values
heap ← empty min-heap of (freq, value) size up to k
for each (value, freq) in count:
  push into heap
  if heap size > k: pop
return heap contents

Narrate why min-heap on frequency: you discard the least frequent when over capacity.

k closest / custom distance

Define the comparison key explicitly:

“Heap entries are (distance, point); I’ll keep a max-heap of size k by distance so I can evict the farthest.”

Language detail: some libraries only give min-heaps—negate distance to simulate max-heap and say so.

First five minutes

  1. Restate: k largest vs k frequent vs k closest
  2. Confirm ties policy if relevant
  3. Choose heap vs sort vs Quickselect with complexity
  4. Define comparison key
  5. Sketch push/pop invariant (“heap always holds candidates”)

Complexity cheat lines worth saying

Pick one and stick to it unless asked to optimize further.

Common bugs

Streaming and “design” flavors

If the prompt is a class with add and getTopK:

For median maintenance, mention two-heap balance briefly; do not implement unless that is the problem.

Freeze recovery

  1. Write the invariant: “heap has at most k best so far”
  2. On paper, process a 5-element array with k=3
  3. Implement push/pop only—no fancy wrappers
  4. If language API is fuzzy, say you will use a sorted list of size k as temporary stand-in, then upgrade

Talk track:

“I’m going to get the size-k eviction right on an example, then code it.”

Practice plan

SessionFocus
1k largest with min-heap
2k most frequent
3k closest points
4Timed: choose heap vs sort from prompt only

Soft practice note

Top-k is a composure pattern: clear invariant, small API surface, honest complexity. If you want a Chrome side panel to rehearse the min-heap-of-size-k talk track while you practice, Luna Interview supports that calm practice loop. Use it to build fluency—not to skip heap fundamentals or to violate interview rules.

Day-of checklist

Keep the heap tiny and the invariant loud—that is the top-k interview pattern.