Most interview problems are a known pattern wearing a costume. Before coding, match the statement against these cues — saying "this smells like X because Y" out loud is exactly what interviewers want to hear.
Cue → pattern
| The problem says… | Reach for | Sheet |
|---|---|---|
| "pair/triplet that sums to…", "have you seen this before?" | Hash map | Arrays & Hashing |
| Sorted array, pair from both ends, in-place dedupe | Two pointers | Two Pointers |
| "longest/shortest substring/subarray with property P" | Sliding window | Sliding Window |
| Sorted anything, "minimum X such that…", answer is monotonic | Binary search | Binary Search |
| Matching brackets, undo/nesting, "next greater element" | Stack / monotonic stack | Stacks |
| Reverse/reorder a list, cycle detection, k-th from end | Linked-list pointers | Linked Lists |
| Anything on a binary tree | DFS recursion or BFS levels | Binary Trees |
| Prefix matching, autocomplete, word dictionary | Trie | Tries |
| "k largest/smallest/closest/most frequent", running median | Heap | Heaps & Top-K |
| Ranges with start/end, "merge", "overlap", meeting times | Intervals | Intervals |
| "All combinations/permutations/ways to…" (n ≤ ~20) | Backtracking | Backtracking |
| Grid regions, connectivity, prerequisites/dependencies | Graph BFS/DFS, topo sort, union-find | Graphs |
| "Number of ways", "min cost to reach", choices with overlap | Dynamic programming | Dynamic Programming |
| "Maximum you can schedule/keep", local choice feels safe | Greedy (prove it!) | Greedy |
| XOR, "appears once", powers of two, count bits | Bit manipulation | Bit Manipulation |
Tie-breakers
- Substring vs subsequence: contiguous → sliding window / two pointers; non-contiguous → DP.
- "All ways" vs "how many ways": enumerate them → backtracking; just count → DP.
- Top-k vs k-th: keep a stream of answers → heap of size k; single k-th once → quickselect or heap.
- Graph shortest path: unweighted → BFS; weighted non-negative → Dijkstra (Network Delay Time); ≤ k hops → Bellman-Ford style relaxation (Cheapest Flights).
- Sorted input is a gift — it usually means two pointers or binary search; if you're not using the sortedness, you're probably missing the intended solution.
When you're stuck
- Restate inputs/outputs and solve a tiny example by hand — the manual process often is the algorithm.
- Say the brute force and its complexity. Then ask: what work is repeated? (→ DP/memo) What information would make each step O(1)? (→ hash map, stack, heap)
- Sort the input mentally — does the problem get easier?
- Check the constraints table in Big-O Complexity — the size of n narrows the pattern to two or three candidates.