The single highest-leverage skill in a coding interview is pattern recognition — matching the shape of a problem to a known technique in under 60 seconds. Below is the architect’s cheat sheet, organized by the signal that should trigger each pattern.
| Pattern | Trigger Signal in the Problem | Time | Space | Example Problems |
|---|---|---|---|---|
| Two Pointers | Sorted array/list, pair/triplet sum, palindrome check | O(n) | O(1) | Two Sum II, 3Sum, container with most water, remove duplicates |
| Sliding Window | Contiguous subarray/substring, “longest/shortest/max/min window” | O(n) | O(k) | Longest substring without repeats, min window substring, max sum subarray of size k |
| Fast & Slow Pointers | Linked list, cycle detection, find middle | O(n) | O(1) | Cycle detection, find middle node, happy number |
| Merge Intervals | Overlapping ranges, scheduling, calendar conflicts | O(n log n) | O(n) | Merge intervals, meeting rooms, insert interval |
| Binary Search | Sorted data, “find in log n”, search space can be halved | O(log n) | O(1) | Search rotated array, find peak element, binary search on answer |
| BFS (Breadth-First) | Shortest path in unweighted graph, level-order, “minimum steps” | O(V+E) | O(V) | Word ladder, rotting oranges, shortest path in grid |
| DFS / Backtracking | Explore all paths/combinations, permutations, subsets, maze | O(2^n) or O(V+E) | O(V) or O(n) | N-Queens, subsets, word search, permutations |
| Topological Sort | Directed graph, dependency ordering, “prerequisites” | O(V+E) | O(V) | Course schedule, build order, task scheduling with dependencies |
| Dynamic Programming (1D) | Optimal substructure, overlapping subproblems, “min/max ways” | O(n) or O(n×k) | O(n) or O(1) | Climbing stairs, house robber, coin change |
| Dynamic Programming (2D) | Two sequences, grid paths, edit distance style | O(m×n) | O(m×n) or O(n) | LCS, edit distance, unique paths |
| Stack-Based (Monotonic Stack) | Next greater/smaller element, histogram, valid parentheses | O(n) | O(n) | Daily temperatures, largest rectangle, valid parentheses |
| Heap / Top-K | “K largest/smallest”, streaming median, merge K sorted lists | O(n log k) | O(k) | Top K frequent, merge K lists, kth largest in stream |
| Union-Find | Connectivity, grouping, cycle detection in undirected graph | O(n α(n)) | O(n) | Number of islands (alt.), accounts merge, redundant connection |
| Prefix Sum / Difference Array | Range sum queries, cumulative totals | O(n) build, O(1) query | O(n) | Subarray sum equals K, range sum query |
| Trie | Prefix matching, word dictionary, autocomplete | O(k) per op | O(ALPHABET×N×k) | Implement Trie, word search II, autocomplete |
| Greedy | Local optimal choice leads to global optimal, interval scheduling | O(n log n) | O(1) | Jump game, gas station, interval scheduling |
| Bit Manipulation | XOR tricks, single number, subsets via bitmask | O(n) or O(2^n) | O(1) | Single number, counting bits, subsets via bitmask |
Decision tree for pattern selection (say this out loud in an interview)
- Is the input sorted or can it be sorted cheaply? → Binary Search or Two Pointers.
- Am I looking for a contiguous subarray/substring with a constraint? → Sliding Window.
- Is this a graph/tree and I need shortest path or level info? → BFS.
- Is this a graph/tree and I need to explore all paths or check connectivity? → DFS/Backtracking or Union-Find.
- Are there dependencies/ordering constraints? → Topological Sort.
- Am I optimizing (min/max/count ways) with overlapping subproblems? → DP — figure out the state, then the transition.
- Do I need the next greater/smaller element, or nested structure validation? → Monotonic Stack.
- Do I need the top/bottom K elements or a running median? → Heap.
- Is this about prefixes of strings? → Trie.
- Can a greedy local choice be proven optimal (exchange argument)? → Greedy — but always sanity check with a counter-example first.