← Back to TypeScript
Heaps and Top-K
The structure behind every "top K" and "kth largest" question — a binary heap where the smallest value always sits on top, no sorting required.
Challenge: build a min-heap
advancedchallengeImplement push, pop and peek over an array-backed binary min-heap — the smallest value is always one index away.
Challenge: the kth largest in a stream
advancedchallengeAnswer kth-largest after every new value with a heap that only ever holds the k largest seen.
Challenge: the most frequent words
advancedchallengeReturn the k most frequent words, ties broken lexicographically — counts in a map, then the exact ordering rule.
Review: the sift that only looks left
advancedreviewA min-heap passes every symmetric test until a node whose right child is the smaller one reaches the top — then priorities pop out of order. Find the blind spot.