The patterns behind most array and subarray problems — walk pointers, slide windows, and precompute totals to turn O(n²) into O(n).
Walk two indices toward each other and skip the wasted work.
O(n)naïve O(n²)When to reach for it
Spot it in the prompt
The problem mentions a sorted array, or you need to compare elements from both ends of the array.
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.
Open full solutionSort by start, then fold overlapping ranges into one.
O(n log n)naïve O(n²)When to reach for it
Spot it in the prompt
The input is intervals represented as start/end pairs, and the task is about combining or manipulating those intervals.
How to solve it
Sort by start time, then walk the list folding each interval into the current one whenever they overlap.
Open full solutionImpose order first so the rest of the problem falls out.
O(n log n)naïve O(n²)When to reach for it
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.
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.
Open full solutionKeep a moving window and update the answer as it slides.
O(n)naïve O(n²)When to reach for it
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".
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.
Open full solutionPrecompute running totals to answer range queries in O(1).
O(1) / querynaïve O(n) / queryWhen to reach for it
Spot it in the prompt
The problem mentions subarray sums, cumulative sums, or range sums, and hints that precomputing sums would speed things up.
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.
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