# Heaps and Priority Queues

> Use binary heaps to repeatedly extract the smallest or largest item in O(log n), and apply them to top-k, merging and scheduling problems. Free notes, key...

Canonical: https://www.internhack.in/roadmaps/tracks/cs-fundamentals/heaps-priority-queues
Last updated: 2026-11-28

Use binary heaps to repeatedly extract the smallest or largest item in O(log n), and apply them to top-k, merging and scheduling problems.

Intermediate · about 5 hours · difficulty 3/5

Learn first: [Binary Trees and Traversals](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-trees)

A **binary heap** is a complete binary tree stored in an array where each parent is smaller (min-heap) or larger (max-heap) than its children. For index i, children are 2i + 1 and 2i + 2 and the parent is (i - 1) / 2.

### Operations

- Push: append and **sift up**, O(log n).
- Pop top: move the last element to the root and **sift down**, O(log n).
- Peek: O(1).
- Build a heap from n elements in O(n) with bottom-up heapify (a classic theory question).

A **priority queue** is the abstract type, and heaps are its usual implementation: `priority_queue` (max-heap by default in C++), `PriorityQueue` (min-heap in Java), `heapq` (min-heap in Python).

### Patterns

- **Top k**: keep a min-heap of size k while scanning; the heap holds the k largest. O(n log k).
- **K-way merge**: merge k sorted lists by always popping the smallest head.
- **Two heaps**: a max-heap for the lower half and a min-heap for the upper half gives the running median.
- **Greedy scheduling**: always process the most urgent task (Task Scheduler, meeting rooms, IPO).
- **Dijkstra's algorithm** uses a min-heap of (distance, node).

### Pitfalls

Forgetting that C++ and Java defaults differ, comparing pairs in the wrong order, and trying to update priorities in place (instead, push a new entry and skip stale ones when popped).

## Key concepts

- **Array representation of a heap**
- **Sift up, sift down, heapify**
- **Library priority queues**
- **Top-k with a size-k heap**
- **K-way merge**
- **Two heaps for running median**
- **Lazy deletion of stale entries**

## Mini project

Implement a min-heap from scratch, then solve 8 problems: Kth Largest Element in an Array, Kth Largest in a Stream, Top K Frequent Elements, K Closest Points to Origin, Merge k Sorted Lists, Last Stone Weight, Task Scheduler, and Find Median from Data Stream.

## Check yourself

To find the k largest elements in a stream, why do you use a min-heap of size k rather than a max-heap?

## Free resources

- [VisuAlgo: Binary Heap](https://visualgo.net/en/heap) (VisuAlgo)
- [Heap data structure](https://www.geeksforgeeks.org/heap-data-structure/) (GeeksforGeeks)
- [NeetCode Roadmap (Heap)](https://neetcode.io/roadmap) (NeetCode)

Previous: [Binary Search Trees](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-search-trees) · Next: [Tries (Prefix Trees)](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/tries)

## More in Trees and Heaps

- [Binary Trees and Traversals](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-trees)
- [Binary Search Trees](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/binary-search-trees)
- [Heaps and Priority Queues](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/heaps-priority-queues)
- [Tries (Prefix Trees)](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/tries)
- [Segment Trees and Fenwick Trees](https://www.internhack.in/roadmaps/tracks/cs-fundamentals/segment-tree-fenwick)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [CS Fundamentals and Placement Prep Roadmap](https://www.internhack.in/roadmaps/tracks/cs-fundamentals)
