Pattern recognition is the skill being tested: match the shape of the problem to a technique in under 60 seconds, then say the match out loud before you write anything.
Key Terms — Pattern Triggers
| Signal in the problem | Reach for |
|---|---|
| Sorted array, pair/triplet sum, palindrome | Two pointers |
| Contiguous subarray, “longest/shortest window” | Sliding window |
| Linked list, cycle, find the middle | Fast & slow pointers |
| Overlapping ranges, scheduling, calendars | Merge intervals |
| Sorted data, “find in log n”, halvable search space | Binary search |
| Shortest path unweighted, level order, “minimum steps” | BFS |
| All paths, permutations, subsets, maze | DFS / backtracking |
| Dependencies, prerequisites, directed graph | Topological sort |
| “K largest/smallest”, streaming median, merge K lists | Heap / top-K |
| Connectivity, grouping, cycle in undirected graph | Union-Find |
| Next greater/smaller, histogram, valid parentheses | Monotonic stack |
| Range sums, cumulative totals | Prefix sum |
| Prefix matching, autocomplete, dictionary | Trie |
| Optimal substructure + overlapping subproblems | DP (1D; 2D for two sequences or grids) |
Numbers That Matter
| Structure / op | Complexity | Note |
|---|---|---|
| Hash map get/put/delete | O(1) avg | Worst case O(n) on pathological collisions |
| Balanced BST | O(log n) | Search, insert, delete |
| Heap push/pop | O(log n) | Peek is O(1) |
| Heapify from array | O(n) | Not O(n log n) — amortized analysis; a favourite follow-up |
| Trie lookup | O(k) | k = key length, independent of dictionary size |
| Graph adjacency list | O(V+E) | The traversal bound to quote |
| LRU / LFU cache | O(1) get and put | Hash map + doubly linked list (LFU adds a freq map) |
| Quickselect | O(n) average | Kth largest without a full sort |
| Top-K frequent via bucket sort | O(n) | Beats the heap solution’s O(n log k) |
Decision Rules
- Say the pattern before you code. “This is a sliding window because we want the longest contiguous substring under a constraint” earns more than silently producing correct code.
- Rate limiter choice: fixed window counter is simplest but allows a 2× burst at window edges; sliding window log is exact but memory-heavy; sliding window counter is the practical compromise with small approximation error; token bucket handles bursts deliberately. Distributed? Move the counter to Redis and be explicit about the race.
- LRU vs LFU: LRU evicts by recency and is right for most caches; LFU evicts by frequency and protects genuinely hot keys, at the cost of a second map and slower adaptation to changing workloads.
- Union-Find for undirected cycles; topological sort (or DFS colouring) for directed ones. Getting this backwards is a common stumble.
- Two heaps for a streaming median — max-heap for the lower half, min-heap for the upper, rebalanced to differ by at most one.
- Architect framing wins points: a trie is autocomplete and IP routing tables; a dependency graph is topological sort and blast-radius analysis for a service mesh; a heap is top-K and a scheduler’s priority queue.
- State the complexity unprompted, both time and space, and say what you’d optimize if the input grew 1000×.
Say This Out Loud
“Before I write anything — this is a sliding window problem. The signal is that we want the longest contiguous substring satisfying a constraint, which means I can maintain a window with two indices and a hash map of counts, expanding right and contracting left, in O(n) time and O(k) space where k is the character set. Let me confirm the input constraints before I start.”
“Heapify from an array is O(n), not O(n log n). The naive analysis multiplies n elements by log n depth, but most nodes are near the bottom of the heap and sift down only a level or two — the amortized sum converges to linear. It’s worth knowing because it’s the difference between building a heap and sorting.”
“For a rate limiter I’d start with a sliding window counter. A fixed window is simpler, but it allows double the intended rate right at the window boundary — two full bursts back to back across the edge. The log is exact but stores a timestamp per request. The counter approximates with a weighted previous window and gets you accuracy within a couple of percent at a fraction of the memory. In a distributed setting that counter lives in Redis, and I’d be explicit about the read-modify-write race and use an atomic script.”