Tags

Min Heap

Min Heap

1 lesson
5 problems
2 code snippets
1 community item

min-heap

Data Structures

1 lesson

Heaps & Priority Queue

Intermediate

55 min

2 prereqs

Pull the next task in priority order from a stream of millions, and a sorted array gives you `O(1)` extract but `O(n)` insert; a sorted linked list flips the costs but stays linear; a balanced BST hits `O(log n)` for both but with substantial pointer overhead. A **Heap** quietly outperforms all three for this exact workload by storing a complete binary tree in a flat array and keeping just the min (or max) at the root. This lesson covers the heap property, the parent-and-child index formulas (`parent = (i-1)/2`, `left = 2i+1`, `right = 2i+2`) that let array indices encode tree structure with no pointers, sift-up and sift-down for `O(log n)` insert and extract, and the linear-time `heapify` build. You will see how this maps onto the priority queue abstraction and onto interview staples such as top-K elements, K-th largest, merge K sorted lists, and the streaming median with two heaps; on the systems side, the same structure powers Dijkstra and Prim. In **Trees: Binary Tree Fundamentals**, you saw what a complete binary tree is and how to traverse one; the heap reuses that exact shape but stores it implicitly. **Arrays & Strings** is what makes the implicit storage cheap: a flat array gives `O(1)` parent and child access through arithmetic, with no allocations per node. Next, **Binary Search Tree (BST)** trades the heap's partial ordering for a full ordering invariant, unlocking sorted iteration and key-based search at the cost of needing actual pointers.

Not Started

0%

Heap
Priority Queue
Min Heap
Max Heap
Heapify
Data Structures
Intermediate
Premium
Trees
Binary Tree

Practice Problems

5 problems

Kth Largest Element in a Stream

Free
Not Started
Easy

Design a class that finds the kth largest element in a stream of numbers using a min-heap.

Heap
Min Heap
Priority Queue
Data Structures
Beginner

350

5

Find Median from Data Stream

Free
Not Started
Hard

Design a data structure that efficiently finds the median from a stream of integers using two heaps.

Heap
Min Heap
Max Heap
Priority Queue
Median Finding
Data Structures
Advanced

712

5

Design Twitter

Free
Not Started
Medium

Design a simplified Twitter with post, follow/unfollow, and a news feed that merges recent tweets from followed users using a heap.

Heap
Min Heap
Priority Queue
Hash Map / Dictionary
Data Structures
Intermediate

638

11

Find K Pairs with Smallest Sums

Free
Not Started
Medium

Given two sorted arrays, find the k pairs with the smallest sums using a min-heap to efficiently explore candidates.

Heap
Min Heap
Priority Queue
Sorting
Intermediate

696

12

Kth Largest Element in an Array

Free
Not Started
Medium

Find the kth largest element in an unsorted array using a min-heap or quickselect algorithm.

Heap
Min Heap
Priority Queue
Quickselect
Sorting
Intermediate

485

2

Code Snippets

2 snippets
Code Snippet
Premium

Dijkstra in Python with heapq

Dijkstra finds the shortest path in a weighted graph with non-negative edge weights. The Python idiom is `heapq` (a binary min-heap) plus a distance dict, which gives O((V + E) log V) without external libraries. This entry covers the standard single-source template, path reconstruction, and the early-exit shortest-path-to-one-target variant.

Python
dijkstra
graph-algorithms
min-heap
algorithms

674

16

Hard
Code Snippet

Min-Heap via std::priority_queue

`std::priority_queue` is a max-heap by default. To get a min-heap you flip the comparator. This snippet shows the three common forms: a min-heap of ints with `std::greater`, a min-heap of pairs sorting by first element, and a custom comparator over a struct for things like Dijkstra. All run in O(log n) per push/pop.

C++
cpp-stl
priority-queue
min-heap
heap

1.1k

35

Medium