Cheatsheet
Pattern recognition
Signals in a problem statement, the technique they usually point to, its typical cost, and a solved problem that shows it.
Version 1 · updated 2026-11-24
Arrays and strings
| Signal in the problem | Technique | Typical time | Example |
|---|---|---|---|
| Sorted input, find a pair or meet a target | Two pointers from both ends | O(n) | Two prices that fit the budget |
| Unsorted input, "does some pair add up to…" | Hash map of complements | O(n) expected | Two readings that add up |
| "Has this been seen before?" | Hash set | O(n) expected | Badge scanned twice |
| Contiguous block of fixed length k | Fixed sliding window | O(n) | Busiest stretch of k hours |
| Longest / shortest contiguous block with a condition that stays true when shrinking | Variable sliding window | O(n) | Longest stretch without repeats |
| Many range sums, or count subarrays summing to k | Prefix sums (+ hash map of seen sums) | O(n) build, O(1) per query | Stretches that add up to k |
| "Product / sum of everything except me" | Prefix and suffix passes | O(n) | Every product but your own |
| Compact or filter in place | Read/write pointers | O(n), O(1) space | Push the empty slots to the end |
| Three groups in one pass | Three-way partition | O(n) | Red, white and blue |
| Best sum of a contiguous run | Kadane’s algorithm | O(n) | Best streak of profits |
| An element occurring more than n/2 times | Majority vote | O(n), O(1) space | More than half the votes |
| Every value appears twice except one | XOR everything | O(n), O(1) space | The sock without a pair |
Stacks, heaps and searching
| Signal in the problem | Technique | Typical time | Example |
|---|---|---|---|
| Next greater / smaller element, "days until…" | Monotonic stack | O(n) | Days until it gets warmer |
| Nested or matching structure (brackets, tags) | Stack | O(n) | Do the brackets close? |
| Sorted array, find a value or its first / last position | Binary search (lower / upper bound) | O(log n) | Where a value starts and ends |
| Smallest x for which a yes/no check flips, and the check is monotonic | Binary search on the answer | O(log range) checks | First broken build |
| Top k, k-th largest, repeatedly take the minimum | Heap of size k | O(n log k) | Merging k sorted lists |
| Sliding window maximum / minimum | Monotonic deque | O(n) | Max of every window of size k |
| Prefix lookups over many strings | Trie | O(L) per word | Autocomplete |
Graphs and search spaces
| Signal in the problem | Technique | Typical time | Example |
|---|---|---|---|
| Fewest steps, unweighted moves, levels | BFS | O(V + E) | Shortest path in a grid |
| Explore everything reachable, count regions | DFS or BFS | O(V + E) | Flood fill, connected components |
| Groups that merge over time, "are a and b connected?" | Union–Find | near O(1) amortized per operation | Dynamic connectivity |
| Tasks with prerequisites | Topological sort | O(V + E) | Build order, course schedule |
| Cheapest path, non-negative weights | Dijkstra with a binary heap | O((V + E) log V) | Road network routing |
| Connect everything at minimum total cost | Kruskal or Prim | O(E log E) / O(E log V) | Cabling a set of buildings |
| All subsets, permutations or placements | Backtracking with pruning | O(2ⁿ) / O(n!) worst | N-Queens, Sudoku |
| Count ways or optimise over choices, same subproblems recur | Dynamic programming | states × transitions | No two neighbours |
| Local choice provably never hurts (exchange argument) | Greedy | often O(n log n) (sort first) | Buy once, sell once |
| Overlapping intervals | Sort by start, then sweep | O(n log n) | Merge meeting times |
Reading the constraints
| n up to | Usually acceptable | Typical techniques |
|---|---|---|
| 10 – 12 | O(n!), O(n² · 2ⁿ) | Permutations, bitmask DP over subsets |
| 20 – 25 | O(2ⁿ), O(n · 2ⁿ) | Subsets, backtracking, meet in the middle at ~40 |
| ~500 | O(n³) | Triple loops, Floyd–Warshall, interval DP |
| ~5,000 | O(n²) | All pairs, 2D DP |
| ~10⁶ | O(n log n) | Sorting, heaps, binary search per element |
| ~10⁸ or more | O(n) or better | One pass, two pointers, math, O(log n) search |
- Before reaching for a pattern, write the brute force and its cost. The pattern is whatever removes the repeated work.
- Sorted input, or the freedom to sort, suggests two pointers or binary search.
- "Contiguous" suggests windows or prefix sums; "subsequence" suggests DP.
- A window only works when shrinking it never makes an invalid window valid again (all-positive sums, at most k distinct). With negative numbers, use prefix sums.