skip to content

Heaps & Priority Queues

Understand the heap as its own interview subject: how the structure works under the hood, how priority queue APIs expose it, and which classic problem shapes it unlocks. Interviewers lean on heaps because one simple invariant connects directly to complexity arguments, library fluency, and pattern recognition.

part ofData structures & algorithmsoverview, primer and where to startread it →
on this pageshow

questions

page 2 of 2

In frequency top-50 over 10 million distinct terms, where does the memory actually go?

level: seniorimportance: should knowfreq 55%

basics

~20 s

Almost all of it goes to the counting phase. Ranking terms by frequency needs a count for every one of the 10 million distinct terms held at once; the size-50 heap adds fifty entries. The heap bounds the output, not the memory.

open as a page

Why does a two-heap running median report a far-too-low value if rebalancing only shrinks the max-heap?

level: seniorimportance: should knowfreq 40%

basics

~20 s

Repairing only the max-heap-too-large case lets the min-heap grow without limit whenever samples arrive above the current median, so the max-heap keeps an old small value on top and the reported median sinks toward it. Both repair directions are required.

open as a page

How do you keep a two-heap median over a fixed trailing window when the expiring sample isn't at a top?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Heaps remove only their top, so expiring samples are deleted lazily: mark the departing value as pending removal, track logical sizes separately from physical heap sizes, rebalance on the logical counts, and discard stale tops before reading the median.

open as a page

Sort, size-k heap, or quickselect — which survives an unbounded stream of price updates?

level: seniorimportance: should knowfreq 45%

basics

~20 s

Only the bounded candidate heap survives. It sees each record once and holds O(k) state, so an answer is available at any moment. Sorting and quickselect are offline: both need the entire input materialized with random access.

open as a page

In an indexed heap's sift-down, the position map is updated for only one side of each swap. What breaks?

level: seniorimportance: should knowfreq 26%

basics

~20 s

The sinking element's map entry goes stale, so a later reprioritization by identity resolves to the wrong array slot and mutates a different element's key. Nothing fails at the moment of the bug; the damage surfaces much later.

open as a page

What does a size-k heap commit you to on a memory-capped gateway ingesting an unbounded sensor stream?

level: principalimportance: should knowfreq 40%

basics

~20 s

A size-k heap commits you to one frozen question. Bounded state and a single pass let the job run under the cap, but every value that lost to the bar is destroyed, so a different k or metric means re-ingesting the stream.

open as a page

Top-k becomes a user-tunable parameter up to the full catalog — which selection strategy do you commit to?

level: principalimportance: should knowfreq 35%

basics

~20 s

Commit to one default with a documented, measured threshold rather than three tuned paths. As k approaches n, O(n log k) converges on O(n log n) and the candidate structure holds O(n) items — above the crossover, just sort.

open as a page

Why can inserting into a priority queue keyed by (priority, sequence, payload) tuples fail outright?

level: middleimportance: nice to knowfreq 32%

basics

~20 s

Composite keys compare component by component, so when priority and sequence both tie, the queue falls through to comparing payloads — and payload records with no defined ordering make that comparison fail. A strictly unique sequence number stops the fall-through.

open as a page

Why is a heap's replace-top cheaper than an extract followed by an insert?

level: seniorimportance: nice to knowfreq 32%

basics

~20 s

Replace-top overwrites the root with the new key and runs a single sift-down. Extract-then-insert runs a sift-down to repair the removal and then a sift-up for the arrival — about twice the tree traversal, plus a needless shrink and regrow of the structure.

open as a page

Why does an implicit array heap outperform a node-per-element pointer tree in a hot loop?

level: seniorimportance: nice to knowfreq 38%

basics

~20 s

Same asymptotics, much better constants. The implicit layout stores only keys, contiguously: no child or parent references, no per-node allocation, and navigation is arithmetic rather than a dependent memory load, so the hot top levels stay resident in cache.

open as a page

How do you choose merge fan-in k when merging thousands of sorted streams under a memory ceiling?

level: principalimportance: nice to knowfreq 28%

basics

~20 s

Memory bounds the fan-in, not comparison cost. Comparisons grow as log k, but every open source needs a resident read buffer, so memory grows linearly in k. Take the largest k whose per-source buffers still read efficiently.

open as a page

When would you not keep an exact two-heap median for per-endpoint latency across a large fleet?

level: principalimportance: nice to knowfreq 26%

basics

~20 s

An exact two-heap median keeps every sample forever, so memory grows without bound per endpoint per instance, and two instances' results cannot be combined into a global median. Under a fleet memory ceiling, prefer a bounded window or a mergeable quantile sketch.

open as a page

A teammate proposes Fibonacci heaps for amortized O(1) decrease-key. How do you decide?

level: principalimportance: nice to knowfreq 20%

basics

~20 s

Ask what share of runtime the priority queue owns and how decrease-keys compare with extractions. Fibonacci heaps win on paper, but their pointer overhead and cache behaviour usually lose to an array-backed indexed heap on real inputs.

open as a page

showing 31–43 of 43