Heaps that keep the best element one pop away — top-K, streaming medians, merges, and Dijkstra.
Keep a heap of size K, ignore the rest.
O(n log k)naïve O(n log n)When to reach for it
Spot it in the prompt
Look for problems where you need to track the K largest or smallest elements while processing the dataset.
How to solve it
Animation: Finding Kth Largest/Smallest techniqueMaintain a min-heap of size K; push each value and pop when it exceeds K, leaving the Kth largest at the root.
Open full solutionCount first, then heap by frequency.
O(n log k)naïve O(n log n)When to reach for it
Spot it in the prompt
Look for problems where you need to track element frequencies and select the top K elements based on occurrence count.
How to solve it
Animation: Top K Frequent Elements techniqueBuild a frequency map, then bucket-sort by count or keep a size-K heap to extract the K most common values.
Open full solutionOne heap of list heads, merge in order.
O(n log k)naïve O(nk)When to reach for it
Spot it in the prompt
Look for problems where you're required to merge multiple sorted sequences while maintaining order.
How to solve it
Sort intervals by start, then sweep and merge each into the previous whenever its start overlaps the running end.
Open full solutionTrack the window extreme without rescanning.
O(n)naïve O(nk)When to reach for it
Spot it in the prompt
Look for problems where you maintain a sliding window of fixed size and efficiently find the maximum or minimum element.
How to solve it
Animation: Sliding Window Maximum/Minimum techniqueMaintain a decreasing deque of indices; the front holds the window max, and stale or smaller indices are dropped.
Open full solutionBuild a structure with a heap at its core.
When to reach for it
Spot it in the prompt
Look for problems where you need to design a data structure using priority queues for various functionalities.
How to solve it
Animation: Design Problems techniqueStore timestamped tweets per user and merge followed users' feeds with a heap to surface the 10 most recent posts.
Open full solutionGreedily place the most frequent item first.
When to reach for it
Spot it in the prompt
Look for problems where you construct or manipulate data structures using specific rules using priority queues.
How to solve it
Animation: Construction and Manipulation techniqueGreedily schedule the most frequent task via a max-heap, filling cooldown gaps with other tasks or idle slots.
Open full solutionAlways expand the cheapest frontier node.
O(E log V)When to reach for it
Spot it in the prompt
Look for problems where you process nodes or edges based on their weights or distances in graph-related scenarios.
How to solve it
Animation: With Graphs techniqueRun Dijkstra from the source with a min-heap of arrival times, then return the maximum settled distance.
Open full solutionFDE Coach is a cohort-based program in frontend, backend, AWS, and AI where you build real products and get referred to 200+ hiring partners. The free live workshop is the fastest way to see how we teach.
750+ engineers trained · frontend, backend, AWS & AI