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.

This patternO(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.

Medium

Kth Largest Element in an Array

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Finding Kth Largest/Smallest technique

Maintain 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

02Pattern

Top K Frequent Elements

Count first, then heap by frequency.

This patternO(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.

Medium

Top K Frequent Elements

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Top K Frequent Elements technique

Build 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

03Pattern

Merge K Lists

One heap of list heads, merge in order.

This patternO(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.

Medium Custom animation

Merge Intervals

LeetCode(opens in a new tab)
Loading animation…

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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

04Pattern

Sliding Window Maximum/Minimum

Track the window extreme without rescanning.

This patternO(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.

Hard

Sliding Window Maximum

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Sliding Window Maximum/Minimum technique

Maintain 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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.

Medium

Design Twitter

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Design Problems technique

Store timestamped tweets per user and merge followed users' feeds with a heap to surface the 10 most recent posts.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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.

Medium

Task Scheduler

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the Construction and Manipulation technique

Greedily 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

07Pattern

With Graphs

Always expand the cheapest frontier node.

This patternO(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.

Medium

Network Delay Time

LeetCode(opens in a new tab)
Loading animation…

How to solve it

Animation shows the With Graphs technique

Run 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.

Practice · 5 problems

Pick a problem to watch the technique run on it. A sparkle icon marks a problem with its own custom animation, and a code icon one you can practise here with graded tests.

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