Cheatsheet
The whole map on one page: which family a problem belongs to, and which signal points at which pattern.
7 families
Array Sweeps
You move indices along a line.
Binary Search
Sorted input, or an O(log n) requirement.
Linear Helpers
Process a sequence with one helper structure.
Heap
Keep only the important few.
Trees & Graphs
Traverse a structure.
Recursion Family
Explore possibilities or optimize.
Hashmap (sidekick)
Always-on tool to turn O(n^2) into O(n).
Signal → pattern (21)
| If you see this signal | Reach for |
|---|---|
| Array Sweeps | |
| Longest/shortest/max/min contiguous subarray or substring | Sliding Window Grow right, shrink left while the window is invalid. |
| Sorted array, find a pair/triplet hitting a target | Two Pointers Converge left/right pointers. |
| Range sums, or subarray sum equals K (with negatives) | Prefix Sum Precompute cumulative sums; range = P[j]-P[i]; pair with a hashmap. |
| Values in 1..n; find duplicate/missing in O(1) space | Cyclic Sort / Index-as-Hash The array is its own hash table. |
| Merge/insert overlapping intervals, meeting rooms | Intervals Sort by start, merge when curr.start <= prev.end. |
| Binary Search | |
| Sorted array, find a boundary, or O(log n) required | Binary Search Find the monotonic yes/no predicate; while lo <= hi. |
| Search in a rotated sorted array | Rotated Binary Search One half is always sorted; decide which to keep. |
| Linear Helpers | |
| Matching/balance/nesting, undo last | Stack Push openers, pop and verify on closers. |
| Next greater/smaller, nearest larger to the right | Monotonic Stack Keep an increasing/decreasing stack; each pop resolves an answer. |
| Linked list cycle/middle/nth-from-end | Fast & Slow Pointers Two pointers at different speeds (tortoise and hare). |
| Heap | |
| Top K / K largest / K closest / K most frequent | Heap / Priority Queue Keep a size-K heap; push/pop is O(log K). |
| Trees & Graphs | |
| Level order, min depth, width per level | Tree BFS Process one whole level at a time with a queue. |
| Root-to-leaf path, subtree logic, validation | Tree DFS Recurse; pass bounds/state down. |
| Islands/regions in a grid, shortest steps | Graph BFS/DFS Flood-fill with a visited set; BFS for shortest. |
| Prerequisites, ordering, can-you-finish, dependencies | Topological Sort Indegree + queue (Kahn); leftovers mean a cycle. |
| Connected groups, friend circles, merge accounts | Union-Find Union elements, count distinct roots; path compression + rank. |
| Recursion Family | |
| Generate all subsets/permutations/combinations | Backtracking Choose, recurse, un-choose; prune early. |
| Number of ways / min cost / max value with overlapping choices | 1-D Dynamic Programming Define state, transition, base case. |
| Two strings: edit distance, LCS, grid paths | 2-D Dynamic Programming Grid of subproblems indexed by (i, j). |
| Palindromic substrings or longest palindromic subsequence | Palindrome DP Expand around each center, or fill a 2-D table. |
| Hashmap (sidekick) | |
| Seen-before, count frequencies, group by key, deduplicate | Hashmap / Set Trade space for O(1) lookups. |