Free beta: 60 days of full access, no card needed.84 seats leftSign up free

We use necessary cookies to run the site (sign-in and language). If you accept, we also load Google Analytics to see which pages are used, and Google reCAPTCHA to keep spam off the contact and bug-report forms. Privacy policy

All patterns

Binary heap / priority queue

O(log n) per op

A heap keeps the smallest value at the very top and puts nothing else in order at all. Pushing and popping each cost log n.

Updated Oct 17, 2026

How does Binary heap / priority queue work?

A heap is a tree kept inside a flat array. The children of index i sit at 2i plus one and two.

The only rule is that a parent beats its children. Siblings are in no order whatsoever.

A push writes at the end, then bubbles the value up. It swaps with the parent while it wins.

A pop takes the root, moves the last item up there, and sinks it. It swaps with the better child until it fits.

Both moves walk one path of the tree. The height is log n, so both cost log n.

Only the root ever means anything. Every other position is arbitrary.

Sources: Wikipedia: Binary heap, Python docs: heapq

  1. [5]Pushing 5, 3 and 8. The first value is the root.
  2. [3, 5]3 is pushed and bubbles above the 5.
  3. [3, 5, 8]8 stays where it lands. It does not beat its parent.
  4. pop returns 3The root is taken away. The last item, 8, moves to the top.
  5. [5, 8]8 sinks below the 5. The new root is the smallest left.
The 3 smallest numbers with a max-heap
kSmallest([5, 1, 8, 3, 9, 2], 3)
the heap as a tree
empty
the same heap as the array it lives in
x = -k = 3heap.length = 0
popped (too big to be among the k smallest)
empty

Keep the 3 smallest numbers of [5, 1, 8, 3, 9, 2] in a max-heap. Its root is the biggest of those kept, so it is the first to go.

Speed race
The 5 largest of the same 40 numbers: sort them all, or keep a heap of 5

Bars: the 40 numbers. Sorting rearranges them all; the heap only reads them, keeps its 5 numbers aside and lights up the one it is considering.

One operation is one comparison or one move. Every frame, each lane does the same number of operations, so the slow one falls behind.

Sort everything O(n log n)
0 operations
Min-heap of 5 O(n log k)
0 operations

 

And with more numbers

Operations for n numbers, and roughly how long they take at 100 million simple operations a second. An estimate from the same count, not a measurement.

n = 1,000
Sort everything19.9K · under 1 ms
Min-heap of 51.2K · under 1 ms
n = 100,000
Sort everything3.3M · 33 ms
Min-heap of 5100.3K · 1 ms
n = 10,000,000
Sort everything465.1M · 4.7 s
Min-heap of 510M · 100 ms

The Binary heap / priority queue code template

function kSmallest(nums, k) {
    const heap = []; // max-heap: keeps the k smallest seen so far
    const swap = (i, j) => ([heap[i], heap[j]] = [heap[j], heap[i]]);
    function push(v) {
        heap.push(v);
        let i = heap.length - 1;
        while (i > 0 && heap[(i - 1) >> 1] < heap[i]) { swap((i - 1) >> 1, i); i = (i - 1) >> 1; }
    }
    function pop() {
        const top = heap[0];
        heap[0] = heap.pop();
        let i = 0;
        while (2 * i + 1 < heap.length) {
            let c = 2 * i + 1;
            if (c + 1 < heap.length && heap[c + 1] > heap[c]) c++;
            if (heap[i] >= heap[c]) break;
            swap(i, c); i = c;
        }
        return top;
    }
    for (const x of nums) {
        push(x);
        if (heap.length > k) pop();
    }
    return heap.slice().sort((a, b) => a - b);
}

A worked example of Binary heap / priority queue

The k closest points to the origin

Return the k points that lie closest to the origin.

The list can be far too long to sort, and only k points are wanted.

Keep a heap of size k whose top is the worst point still kept.

Push each point, and pop the top whenever the heap grows past k. What remains is the answer.

function kClosest(points, k) {
    // a max-heap keyed by squared distance: the worst kept point sits on top
    const heap = new MaxHeap((p) => p[0] * p[0] + p[1] * p[1]);

    for (const point of points) {
        heap.push(point);

        if (heap.size() > k) {
            heap.pop(); // drops the farthest, never the closest
        }
    }

    return heap.toArray();
}

When should you use Binary heap / priority queue?

Use Binary heap / priority queue when the problem statement says “always need the current smallest/largest”, “top k elements” or “merge k sorted lists”. A solution built on it usually runs in O(log n) per op.

  • always need the current smallest/largest
  • top k elements
  • merge k sorted lists
  • running median while items keep arriving
  • priority queue / most urgent next

The signal words of every pattern sit in one table, the algorithm patterns cheat sheet.

What is Binary heap / priority queue confused with?

Binary heap / priority queue is most often confused with Fast sort (merge / quick), Binary search tree, Monotonic deque (sliding window max/min) and Dijkstra's algorithm.

Common mistakes with Binary heap / priority queue

  • Expecting a sorted array

    Only the root is in any order. Reading the array from front to back gives nonsense.

  • Keeping every element for a top-k question

    A heap of size k costs O(n log k). Holding all of them costs more memory for no gain.

  • Building the heap the wrong way round

    For the k largest you need a min-heap, so the worst kept sits on top. That is easy to invert.

  • Trying to delete an arbitrary element

    A heap has no cheap way to find one. Mark it dead and skip it when it surfaces.

Interview problems solved with Binary heap / priority queue include Kth largest element in a stream, K closest points to origin, Merge k sorted lists, Top k frequent elements, Task scheduler, Find median from a data stream and Dijkstra's shortest path.

  • Kth largest element in a stream: A min-heap of size k, and the root is the answer.
  • K closest points to origin: A bounded max-heap over squared distance.
  • Merge k sorted lists: The heap holds the front node of every list.
  • Top k frequent elements: Counts first, then a heap of size k.
  • Task scheduler: The most frequent task goes first each round.
  • Find median from a data stream: Two heaps facing each other across the middle.
  • Dijkstra's shortest path: The heap decides which node to settle next.

What is the time and space complexity of Binary heap / priority queue?

The target complexity of Binary heap / priority queue is O(log n) per op.

n up to 1e6 gives O(log n) per push or pop. Reading the top alone is O(1).

See where this fits in the 150-step track