Patterns
16 reusable ways of attacking a problem. Each page says how to recognise the pattern from the statement, when it does not apply, gives a language-neutral template, works through a small example, and links to the solved problems, lessons and visualizers that practise it.
Arrays and strings
- Two pointers
Walk two indices through a sequence, from both ends or at two speeds, so every step rules out candidates and one pass replaces a nested loop.
O(n) · beginner · 10 solved problems
- Sliding window
Keep a contiguous range and a running summary of it; extend the right edge, shrink the left edge, and never recompute the range from scratch.
O(n) · intermediate · 2 solved problems
- Prefix sums
Precompute running totals once so any range sum is one subtraction, and combine them with a hash map to count ranges that hit a target.
O(n) build, O(1) per query · intermediate · 3 solved problems
- One pass with a running state
Carry a small summary of everything seen so far (a best, a count, a candidate) so each element is decided in O(1) and the whole scan is linear.
O(n) · beginner · 6 solved problems
Searching and lookup
- Binary search on a monotonic predicate
When a yes/no question flips exactly once over a range, binary search finds the boundary in O(log range) checks, whether the range is indices or answers.
O(log R × cost of ok) · intermediate · 2 solved problems
- Hashing for constant-time lookup
Trade memory for time: remember what you have seen in a hash set or map so "have I seen x?" or "where was its complement?" costs expected O(1).
O(n) expected · beginner · 9 solved problems
- Bit tricks and arithmetic invariants
Use algebra instead of storage: XOR cancels pairs, sums reveal a missing value, masks test bits, and Euclid shrinks a gcd in logarithmic steps.
O(n) scans; O(set bits) popcount; O(log min(a, b)) gcd · intermediate · 6 solved problems
Stacks and heaps
- Monotonic stack
Keep a stack whose values only increase (or only decrease); each new element pops everything it beats, answering "next greater" for all of them at once.
O(n) · intermediate · 2 solved problems
- Top K with a heap
Keep only the k best items in a heap of size k; each new item costs O(log k), so the k largest of n come out in O(n log k) without sorting everything.
O(n log k) · intermediate · 2 solved problems
Trees and graphs
- Breadth-first search and level order
Explore in rings around the start with a FIFO queue; the first time you reach a node is by a fewest-edges path, and one ring is one level.
O(V + E) · intermediate · 2 solved problems
- Depth-first search and backtracking
Go as deep as possible before turning back; with choose, explore and undo, the same recursion enumerates subsets, permutations and constrained placements.
O(V + E) for graph DFS; O(2ⁿ) subsets, O(n!) permutations · intermediate · 1 solved problem
- Union-find for connectivity
Track which items are connected as edges arrive: union merges two groups, find names a group, and both are nearly constant time with rank and path compression.
O(α(n)) amortized per operation · intermediate · 2 solved problems
- Topological ordering
Order tasks so every dependency comes first: repeatedly take a task with no unmet prerequisites, and if none is left early the graph has a cycle.
O(V + E) · intermediate · 1 solved problem
Optimization
- Sort and merge intervals
Sort intervals by start, then sweep once: each interval either extends the current merged block or starts a new one.
O(n log n) · intermediate · 2 solved problems
- Greedy choice after sorting
Order the candidates so the locally best choice is safe, then commit to it; an exchange argument, not intuition, proves the greedy answer optimal.
O(n log n) · advanced · 6 solved problems
- Dynamic programming (1D and 2D)
Name the state, write the transition from smaller states, fix the base cases, then fill a table in dependency order so each subproblem is solved once.
O(states × transitions) · advanced · 5 solved problems
Prefer a guided order? The learning paths and theinterview sequences put these patterns in sequence.