Patterns
A cross-cutting map of the techniques in this guide, and how to recognise which one a problem wants
Most interview problems are variations on a small number of techniques. Learning to recognise the pattern is worth more than memorising any individual solution — and it is what turns an unfamiliar problem into a familiar one.
This page maps every technique in the guide to the problems that teach it.
The Recognition Table
Start here. The wording of a problem usually points straight at the technique.
| The problem says… | Reach for | Start with |
|---|---|---|
| "have I seen this / find a pair" | Hash map or set | Two Sum |
| "the array is sorted" | Two pointers or binary search | Merge Sorted Array |
"subarray of size k" | Fixed sliding window | Maximum Average Subarray I |
| "sum over a range" | Prefix sums | Running Sum of 1d Array |
"in place, O(1) space" | Read and write pointers | Remove Duplicates II |
| "matching / nesting / undo" | Stack | Valid Parentheses |
| "level by level / fewest steps" | BFS with a queue | Level Order Traversal |
| "explore every path / subtree" | DFS with recursion | N-ary Preorder |
| "how many ways / min cost" | Dynamic programming | Climbing Stairs |
| "can I reach / is it possible" | Greedy, if provable | Jump Game |
| "find the first X where…" | Binary search on a boundary | First Bad Version |
"k most frequent / largest" | Counting + bucket or heap | Top K Frequent |
| "group things that are equivalent" | Canonical key + hash map | Group Anagrams |
"support these ops in O(1)" | Combine structures | Insert Delete GetRandom |
The Ten Patterns
1. Hash Map / Set — trade memory for time
Turn an O(n) search into an O(1) lookup. The single highest-leverage move in this guide.
Two Sum · Contains Duplicate · Group Anagrams · Valid Anagram · Minimum Index Sum · Unique Email Addresses
2. Two Pointers — converging
Start at both ends and move inward under a rule that provably discards possibilities.
Container With Most Water · Valid Palindrome · Trapping Rain Water
3. Two Pointers — same direction
Both pointers move forward, at different rates or under different conditions.
Is Subsequence · Remove Duplicates II · Merge Sorted Array
4. Fast & Slow Pointers
One pointer moves twice as fast. Finds middles, detects cycles, locates offsets.
Middle of the Linked List · Linked List Cycle II · Intersection of Two Lists
5. Sliding Window
A range that slides or grows across the data. Fixed size for "subarray of size k"; variable size for "longest such that…".
6. Prefix / Suffix Precomputation
One pass forward, one pass back, then combine. Turns range queries into O(1) arithmetic.
Running Sum · Find Pivot Index · Product of Array Except Self · Candy · Trapping Rain Water
7. Stack
Last-in-first-out. Nesting, undo, deferred operands, depth-first traversal.
Valid Parentheses · Min Stack · Evaluate RPN · Simplify Path
8. BFS / DFS
Queue for breadth, stack for depth. The choice determines both the visit order and the space cost.
Level Order Traversal · N-ary Preorder · Jump Game II
9. Binary Search
Halve the search space. Works for exact matches, for boundaries, and even when there is no array.
Binary Search · Search Insert Position · First Bad Version
10. Greedy vs. Dynamic Programming
Greedy when the local choice is provably safe; DP when it is not.
Jump Game · Gas Station · Candy · Integer to Roman · Climbing Stairs
Complexity Cheat Sheet
| Notation | Meaning | Example |
|---|---|---|
O(1) | constant | hash map lookup |
O(log n) | halving each step | binary search |
O(n) | one pass | a single loop |
O(n log n) | sorting | comparison sorts |
O(n²) | nested loops | brute-force pairs |
O(2ⁿ) | branching recursion | unmemoized subsets |
Constraints tell you the target complexity
n ≤ 10⁵ rules out O(n²) — that would be 10 billion operations. It points at O(n) or O(n log n).
n ≤ 20 is small enough for exponential, which usually means the intended solution is backtracking or bitmasking.
Reading the constraints first often tells you which technique the author had in mind.