7 patterns · 35 problems
Priority Queues
Heaps that keep the best element one pop away — top-K, streaming medians, merges, and Dijkstra.
Each pattern below starts with when to reach for it and the clue in the prompt that gives it away. Then it runs on real problems: pick one to watch the technique step by step, open its full solution, or practise it on an original problem with graded tests.
01Pattern
Finding Kth Largest/Smallest
Keep a heap of size K, ignore the rest.
O(n log k)naïve O(n log n)When to reach for it
- You have a large dataset, and you need to efficiently find the Kth largest or smallest element.
Spot it in the prompt
Look for problems where you need to track the K largest or smallest elements while processing the dataset.
Kth Largest Element in an Array
How to solve it
Animation shows the 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.
Kth Highest Daily Swing is our own problem on the same technique: a new statement, graded in your browser.
02Pattern
Top K Frequent Elements
Count first, then heap by frequency.
O(n log k)naïve O(n log n)When to reach for it
- You're analyzing data and need to identify the most frequently occurring elements.
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.
Top K Frequent Elements
How to solve it
Animation shows the 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.
Most Reported Calibration Offsets is our own problem on the same technique: a new statement, graded in your browser.
03Pattern
Merge K Lists
One heap of list heads, merge in order.
O(n log k)naïve O(nk)When to reach for it
- You have K sorted lists and need to merge them into a single sorted list.
Spot it in the prompt
Look for problems where you're required to merge multiple sorted sequences while maintaining order.
Merge Intervals
How to solve it
Sort intervals by start, then sweep and merge each into the previous whenever its start overlaps the running end.
Pipeline Sensor Coverage is our own problem on the same technique: a new statement, graded in your browser.
04Pattern
Sliding Window Maximum/Minimum
Track the window extreme without rescanning.
O(n)naïve O(nk)When to reach for it
- You need to find the maximum or minimum element in all contiguous subarrays of size K.
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.
Sliding Window Maximum
How to solve it
Animation shows the Sliding Window Maximum/Minimum techniqueMaintain a decreasing deque of indices; the front holds the window max, and stale or smaller indices are dropped.
Rolling Peak Readings is our own problem on the same technique: a new statement, graded in your browser.
05Pattern
Design Problems
Build a structure with a heap at its core.
When to reach for it
- You're tasked with designing a custom data structure using priority queues to solve specific problems efficiently.
Spot it in the prompt
Look for problems where you need to design a data structure using priority queues for various functionalities.
Design Twitter
How to solve it
Animation shows the Design Problems techniqueStore timestamped tweets per user and merge followed users' feeds with a heap to surface the 10 most recent posts.
06Pattern
Construction and Manipulation
Greedily place the most frequent item first.
When to reach for it
- You need to construct, modify, or manipulate data structures using priority queues efficiently.
Spot it in the prompt
Look for problems where you construct or manipulate data structures using specific rules using priority queues.
Task Scheduler
How to solve it
Animation shows the Construction and Manipulation techniqueGreedily schedule the most frequent task via a max-heap, filling cooldown gaps with other tasks or idle slots.
Press Die Cooldown is our own problem on the same technique: a new statement, graded in your browser.
07Pattern
With Graphs
Always expand the cheapest frontier node.
O(E log V)When to reach for it
- You need to solve graph-related problems efficiently using priority queues for operations like Dijkstra's algorithm or finding minimum spanning trees.
Spot it in the prompt
Look for problems where you process nodes or edges based on their weights or distances in graph-related scenarios.
Network Delay Time
How to solve it
Animation shows the With Graphs techniqueRun Dijkstra from the source with a min-heap of arrival times, then return the maximum settled distance.
Relay Broadcast Time is our own problem on the same technique: a new statement, graded in your browser.
After the pattern
Now solve one you haven't seen.
Reading a pattern isn't the same as using it. The practice set has 1,194 original problems on the same techniques, each graded in your browser.
1,194 problems · 15,062 graded tests · free