5 patterns · 24 problems
Arrays
The patterns behind most array and subarray problems — walk pointers, slide windows, and precompute totals to turn O(n²) into O(n).
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
Two Pointers
Walk two indices toward each other and skip the wasted work.
O(n)naïve O(n²)When to reach for it
- You need to iterate with two indices, typically starting from opposite ends or different positions in the array.
- The task involves comparing or manipulating elements from two different parts of the array at the same time.
Spot it in the prompt
The problem mentions a sorted array, or you need to compare elements from both ends of the array.
Two Sum
How to solve it
On a sorted array, put one pointer at each end and move them inward: if the pair sums too high move the right pointer left, too low move the left pointer right.
Ledger Reconciliation is our own problem on the same technique: a new statement, graded in your browser.
02Pattern
Merge Intervals
Sort by start, then fold overlapping ranges into one.
O(n log n)naïve O(n²)When to reach for it
- The problem involves intervals or ranges — time slots, scheduling, or overlapping events.
- You need to combine, compare, merge, or find intersections between intervals.
Spot it in the prompt
The input is intervals represented as start/end pairs, and the task is about combining or manipulating those intervals.
Merge Intervals
How to solve it
Sort by start time, then walk the list folding each interval into the current one whenever they overlap.
Pipeline Sensor Coverage is our own problem on the same technique: a new statement, graded in your browser.
03Pattern
Sorting
Impose order first so the rest of the problem falls out.
O(n log n)naïve O(n²)When to reach for it
- Arranging elements in a specific order simplifies searching, counting, or comparing them.
- Sorting by some criteria unlocks the solution or optimizes the operations that follow.
Spot it in the prompt
The problem hints the array needs to be sorted first, or that ordering the data would make the solution obvious.
Sort Colors
How to solve it
The Dutch national flag algorithm: low, mid and high pointers partition 0s, 1s and 2s into place in a single pass.
Triage Band Ordering is our own problem on the same technique: a new statement, graded in your browser.
04Pattern
Sliding Window
Keep a moving window and update the answer as it slides.
O(n)naïve O(n²)When to reach for it
- The problem involves contiguous subarrays or substrings where you slide a window across the input.
- You need to track a subset of elements that satisfies a specific condition as it moves.
Spot it in the prompt
The input involves contiguous subarrays, a "window" that slides across the array, or properties like "maximum sum" or "minimum length".
Minimum Size Subarray Sum
How to solve it
Grow the window to the right until the sum reaches the target, then shrink it from the left as far as possible — the shortest valid window is the answer.
Fewest Shifts to Clear the Quota is our own problem on the same technique: a new statement, graded in your browser.
05Pattern
Prefix Sums
Precompute running totals to answer range queries in O(1).
O(1) / querynaïve O(n) / queryWhen to reach for it
- You need cumulative sums or averages over subarrays, or must answer many subarray-sum queries efficiently.
- Precomputing aggregate values up front can cut the time complexity of repeated range operations.
Spot it in the prompt
The problem mentions subarray sums, cumulative sums, or range sums, and hints that precomputing sums would speed things up.
Range Sum Query - Mutable
How to solve it
Prefix sums answer any range in O(1), but a single update forces you to rebuild them in O(n) — a Fenwick (binary indexed) tree keeps both query and update logarithmic.
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