Heaps

Priority queue backed by a binary heap for fast min or max access.

What is a heap?

A heap is a priority queue shaped like a complete binary tree and stored flat in an array. You do not keep every value sorted — you only promise that the best value sits at the root. Reading that best value is instant; putting a new value in the right place costs a short climb or slide along parent–child links.

Priority
The “best” value at the root — smallest for a min-heap, largest for a max-heap
Parent / child indexes
For index i: parent Math.floor((i - 1) / 2), left 2 * i + 1, right 2 * i + 2
Min vs max
Same tree shape; only the compare direction flips. Interviews usually ask for a min-heap unless they say otherwise.

See it as a tree and an array

Draw the heap as a tree and you see parents watching their children. Lay the same values left-to-right in an array and you see why the formulas work — level order fills the row without gaps.

Watch a parent light up with both children. The array cells with the same indexes flash together. Same structure, two views.

Min-heap · tree and array stay in sync
Focusroot at i0Link

Parent at i · children at 2i+1 and 2i+2. No holes until the last level.

Types of heaps

Most interview heaps are binary heaps. You pick the order — min or max — and keep the tree complete so the array stays packed from the left.

Exotic cousins (binomial, Fibonacci) show up in textbooks. Day-to-day coding interviews almost always mean “array-backed binary heap.”

How heaps are classified
FocusHeap

Order answers “what is best?” Shape answers “how do we store it?”

How it is stored in memory

TypeScript keeps a reference on the stack and the values in one contiguous array on the heap. There are no left/right pointers — parent and child are pure index math on that array.

Tap Next to walk from “empty binding” to “read a child by formula.”

Declare → fill → index math
StatusPress Next to declare heap

Start here. Each step highlights the TypeScript below.

Step 1 of 5

Operations

Four moves cover almost every heap interview: push with sift-up, peek the root, extract with sift-down, and heapify an unordered list. Tap Next on each demo — watch the value bubble through swaps. For copy-paste helpers, open the Functions tab.

Insert + sift-up

Drop the new value at the next open slot (the end of the array). If it outranks its parent, swap and keep climbing until the invariant holds — or you hit the root.

Insert 1 into a min-heap
Statusready

Press Next to append 1 at the end.

Step 1 of 5

Peek

Read the root. You do not walk the tree — index 0 is always the current best. Nothing moves.

Read the best value
Statusready

Press Next to peek at heap[0].

Step 1 of 2

Extract + sift-down

Save the root, move the last value into the hole, then slide that value down — always swapping with the better child — until every parent beats its kids again.

Extract-min from a min-heap
Statusready

Press Next to pull the root and sift down.

Step 1 of 5

Heapify

Start from the last parent and sift each one down. Bottom-up heapify builds a valid heap in linear time — cheaper than pushing every value one by one.

Build a min-heap from [9, 5, 3, 2, 7]
Statusready

Press Next to sift parents from the bottom up.

Step 1 of 4