Tags

Priority Queue

Priority Queue

2 lessons
9 problems
1 code snippet
2 question banks
1 system design
5 community items

priority-queue

Data Structures

2 lessons

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

Van Emde Boas Tree

Advanced

70 min

2 prereqs

For a set of 32-bit integers, balanced BSTs answer successor and predecessor queries in `log_2(2^32) = 32` comparisons, and hash tables cannot answer those queries at all. A **Van Emde Boas (vEB) tree** answers them in `log_2(log_2(2^32)) = 5`, by recursively halving the bit-length of the universe at each level rather than halving the number of elements. That doubly-logarithmic bound is one of the most striking results in data structure theory. This lesson covers the recursive vEB structure (an integer universe `[0, u-1]` is split into `sqrt(u)` clusters of size `sqrt(u)` plus a summary structure over which clusters are non-empty, applied recursively), the explicit `min` and `max` fields that short-circuit the recursion to a single level when possible, the `O(log log u)` analysis for insert, delete, member, successor, predecessor, min, and max, and the `O(u)` space cost that makes vEB trees practical only for moderate universes. In **Binary Search Tree (BST)**, the height (and thus the operation cost) was governed by the number of stored keys; the vEB tree is governed instead by the size of the integer universe, a fundamentally different parameter. **Heaps & Priority Queue** introduced fast min and max access through a structural invariant; vEB trees push that idea further by also providing fast `successor` and `predecessor` over the same set. With Van Emde Boas, the data structures track is complete; the algorithms track is where these structures come alive in real problem solving.

Not Started

0%

Van Emde Boas Tree
Trees
Data Structures
Advanced
Premium
Bit Manipulation
Priority Queue
Searching

Practice Problems

9 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

Last Stone Weight

Free
Not Started
Easy

Simulate smashing the two heaviest stones together repeatedly using a max-heap and return the weight of the last remaining stone.

Heap
Max Heap
Priority Queue
Simulation
Beginner

187

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

IPO

Free
Not Started
Medium

Maximize capital after completing at most k projects by greedily selecting the most profitable affordable projects using a max-heap.

Heap
Max Heap
Priority Queue
Greedy
Sorting
Intermediate

285

2

K Closest Points to Origin

Free
Not Started
Medium

Find the k closest points to the origin using a max-heap to efficiently track the k smallest distances.

Heap
Max Heap
Priority Queue
Sorting
Intermediate

429

10

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

Task Scheduler

Free
Not Started
Medium

Determine the minimum number of intervals needed to execute all tasks given a cooldown period between identical tasks.

Heap
Max Heap
Priority Queue
Greedy
Frequency Count
Intermediate

745

11

Question Banks

2 items
Question Bank

Heap and Priority Queue Quiz

Multi-step prompts on binary heaps: sift-up vs sift-down, heapify cost, top-K extraction, and the array layout that makes parent/child arithmetic O(1).

JavaScript
Python
heap
priority-queue
data-structures
interview-prep

213

1

Medium
Question Bank
Premium

External Sorting and Merge Pass

Disk-aware sorting when data exceeds memory. Drills cover run generation, k-way merge with a heap, and pass-count math.

Python
sorting
merge-sort
priority-queue
interview-prep

451

4

Hard