πŸš€ Phases 1–5 are live β€” Days 1–17 cover the foundations and the algorithmic patterns. See the roadmap β†’

Basic Heap Questions

Six warm-up exercises to lock in heap operations before you take on the interview problems. Each one tests a different muscle β€” the heap property, the index arithmetic, the build vs push tradeoff, the size-K trick.

If you can do these in your head, the practice problems will feel mechanical.

1. Is this a valid min-heap?

For an array to represent a valid min-heap, every parent at index i must be ≀ both arr[2i+1] (left child) and arr[2i+2] (right child) β€” for every index that has children.

Check [3, 5, 9, 6, 8, 20, 10, 12, 18, 9]:

3[0]5[1]9[2]6[3]8[4]20[5]10[6]12[7]18[8]9[9]
array view
3
[0]
5
[1]
9
[2]
6
[3]
8
[4]
20
[5]
10
[6]
12
[7]
18
[8]
9
[9]
MIN-HEAP β€” root is at index 0; children of i are at 2i+1 and 2i+2

2. Trace this sequence on an empty min-heap

Start with []. Apply: push(5), push(3), push(8), push(1), pop(), push(2), pop(), pop().

What does the heap look like at each step? What does each pop return?

3. Build a max-heap from [4, 10, 3, 5, 1] β€” but two ways

Compare cost: building by n successive pushes vs heapify in place.

4. What’s at index 5 in a complete binary tree of 11 nodes?

If the tree is stored at indices 0..10, where is the node at array index 5? Who is its parent? Children?

5. K smallest using a heap β€” which kind?

You want the K smallest elements of a stream. Should you use a min-heap or a max-heap? What size?

6. After this max-heap pop, what does the heap look like?

Start with [20, 15, 17, 8, 10, 5, 12] (max-heap). Pop the root. Show the heap after the operation.

20[0]15[1]17[2]8[3]10[4]5[5]12[6]
array view
20
[0]
15
[1]
17
[2]
8
[3]
10
[4]
5
[5]
12
[6]
MAX-HEAP β€” root is at index 0; children of i are at 2i+1 and 2i+2

Quick check

Which of these is a valid min-heap?
What's the height of a complete binary heap with 1000 elements?

Now play with it

The interactive heap from the Introduction page is right here, too. Try the exercises above, then make your own β€” push 10 random values, then pop them all and watch the sorted output emerge.

Try it: Interactive Heap
(empty heap)
array view
(empty)
MIN-HEAP β€” root is at index 0; children of i are at 2i+1 and 2i+2

Ready for the interview classics? Head to Practice Questions.