A heap is the right tool when you repeatedly ask:
- What is the smallest so far?
- What is the largest so far?
- What should be processed next by priority?
Interviewers often test heaps through top-k, merge, and scheduling problems.
Min-heap vs max-heap strategy
- Keep a min-heap of size
kfor top-k largest elements - Keep a max-heap for repeated extraction of largest priority
In JavaScript and TypeScript, you usually implement a custom heap class because there is no built-in priority queue.
Minimal binary heap (TypeScript)
class MinHeap {
private data: number[] = [];
size(): number {
return this.data.length;
}
peek(): number | undefined {
return this.data[0];
}
push(value: number): void {
this.data.push(value);
this.bubbleUp(this.data.length - 1);
}
pop(): number | undefined {
if (this.data.length === 0) return undefined;
const root = this.data[0]!;
const last = this.data.pop()!;
if (this.data.length > 0) {
this.data[0] = last;
this.bubbleDown(0);
}
return root;
}
private bubbleUp(index: number): void {
while (index > 0) {
const parent = Math.floor((index - 1) / 2);
if (this.data[parent]! <= this.data[index]!) break;
[this.data[parent], this.data[index]] = [this.data[index]!, this.data[parent]!];
index = parent;
}
}
private bubbleDown(index: number): void {
const n = this.data.length;
while (true) {
let smallest = index;
const left = 2 * index + 1;
const right = 2 * index + 2;
if (left < n && this.data[left]! < this.data[smallest]!) smallest = left;
if (right < n && this.data[right]! < this.data[smallest]!) smallest = right;
if (smallest === index) break;
[this.data[index], this.data[smallest]] = [this.data[smallest]!, this.data[index]!];
index = smallest;
}
}
}
Pattern 1: Top-k largest elements
export function topKLargest(nums: number[], k: number): number[] {
const heap = new MinHeap();
for (const value of nums) {
heap.push(value);
if (heap.size() > k) heap.pop();
}
const result: number[] = [];
while (heap.size() > 0) result.push(heap.pop()!);
return result.reverse();
}
Time is O(n log k), better than sorting when k << n.
Pattern 2: Kth largest in stream
Maintain a min-heap of size k. Heap root is current kth largest.
Pattern 3: Merge k sorted lists or arrays
Push first element of each list into heap keyed by value, pop smallest, then push next from same list. Complexity:
O(totalElements * log k).
Pattern 4: Scheduling by earliest finish or highest priority
Many scheduling tasks use heaps with two passes:
- Sort jobs by start time
- Use heap to track active jobs or next completion
Examples: meeting rooms II, CPU task scheduling with cooldown, process stream by priority.
Common mistakes
- Building a max-heap when min-heap of size
kis simpler - Forgetting to cap heap size in top-k problems
- Using full sort (
O(n log n)) where heap can doO(n log k) - Comparator bugs that invert ordering
Practice set
- Kth largest element in array
- Top k frequent elements
- Find median from data stream
- Merge k sorted lists
- Meeting rooms II
Heaps are about repeated priority decisions. If a problem keeps asking for current min or max under inserts, reach for
priority queues early.
