awesome-everything RU
↑ Back to the climb

Algorithms from zero

Heaps: multiple-choice review

Crux Multiple-choice synthesis across the heaps unit: the heap invariant, O(n) build-heap, the PQ contract, and choosing the right heap for top-k and k-way merge.
Your altitude — climbing toward senior
ZeroJuniorMiddleSenior
You are at middle altitude — in the sky
◷ 13 min

Six questions that cut across the whole unit. None are definition recall — each is a design decision: which heap, which complexity, which invariant. Pick the answer you would defend in an interview, then read why each distractor fails.

Goal

Confirm you can connect the heap invariant, the O(n) build-heap argument, the priority-queue contract, and the size-k heap pattern that powers both top-k and k-way merge — the synthesis the five lessons built toward.

Quiz

An interviewer hands you the array [10, 5, 20, 15, 30] and asks 'is this a valid min-heap?' What is the correct answer and the reason?

Quiz

Building a heap from an n-element array by calling push n times is O(n log n), but the standard build-heap (heapify) is O(n). Where does the speedup come from?

Quiz

A teammate says 'a priority queue is just a heap.' Why is that imprecise, and when does the distinction earn its keep?

Quiz

You must keep the k largest elements out of a stream of n numbers (k ≪ n). Which heap, and why that one and not the other?

Quiz

In k-way merge of k sorted lists with n total elements, each heap entry stores (value, listIndex, position) rather than just the value. Why carry the bookkeeping?

Quiz

A pop on a min-heap moves the last array element to the root, shrinks the array by one, then sift-downs. Why move the LAST element up rather than promoting the smaller child into the gap and repeating?

Recap

The through-line of the unit is one structure earning many jobs. The heap property plus the complete-tree shape give O(1) peek and O(log n) push/pop via sift-up and sift-down; build-heap exploits the height distribution to reach O(n). Wrap that in the priority-queue contract and the same machine becomes a scheduler, a top-k filter (size-k MIN-heap, evict the root), and a k-way merger (one candidate per list, advance by listIndex). Every question here was really ‘which invariant or which heap,’ which is exactly the call you make in an interview or a real system.

Continue the climb ↑Heaps: free-recall review
shortcuts expand
search
K
prev piece
k
next piece
j
cycle tier
t
this menu
?
sources3
expand
  1. 01
  2. 02
  3. 03

Trademarks belong to their respective owners. Editorial reference only.