15 / 16
Coding Interview Patterns
The ~20 patterns behind most coding rounds, with the code to recognize and write them.
Big-O says how time or memory grows with input size, ignoring constants: before coding, name n, state time and space, and check them against the input limits.
A hash map gives O(1) average lookup by key, so "have I seen X?", "how many X?" and, with prefix sums, "how many subarrays sum to k?" each take one O(n) pass.
Precompute running totals once in O(n) and any range sum is one subtraction, O(1); the mirror trick, a difference array, makes each range update O(1).
Move two indexes through a sorted array, string or linked list so every step discards candidates for good: O(n) time, O(1) space, where brute force checks all O(n²) pairs.
Keep a contiguous window over an array or string and update its state as the edges move; each element enters and leaves once, so subarray and substring scans drop from O(n²) to O(n).
Halve a sorted range each step to find a value, or the first point where a monotonic yes/no predicate flips, in O(log n) checks: ~30 even for a 10⁹-wide answer space.
A linked list gives up index access to get O(1) insert and delete at a known node; interview problems are pointer surgery with three tools: a dummy head, in-place reversal and fast/slow pointers.
Most tree problems are one traversal: a DFS where each node combines what its children return, or a BFS level by level; in a BST, inorder visits the keys in sorted order.
A stack holds the items still waiting for an answer; a monotonic stack pops every item the new one beats, so 'next greater or smaller' problems drop from O(n²) to O(n).
A heap returns the smallest (or largest) item in O(1) and inserts or removes in O(log n); a size-K heap gives top-K in O(n log K) without sorting everything.
Sort intervals by start, then sweep left to right comparing each one with the last kept interval; for 'how many at once', sweep +1/−1 start and end events instead.
BFS explores level by level with a queue and finds shortest paths when every edge costs the same; DFS goes deep with a stack or recursion; with a visited set, both are O(V + E).
Order a directed graph so every edge points forward: repeatedly take a node with no remaining prerequisites (Kahn), or reverse the DFS finish order; nodes left over mean a cycle.
Track groups as trees with one root each: `find` returns the root, `union` links two roots, and with path compression plus union by rank both run in near-constant amortized time.
Choose by edge weights: BFS when all edges cost the same, Dijkstra with a min-heap when weights are non-negative, Bellman-Ford when some are negative or you must detect a negative cycle.
When the best answer is built from best answers to smaller subproblems, and those subproblems repeat, solve each one once and store it: exponential recursion becomes polynomial.
Take the locally best choice at each step and never undo it; it is correct only when you can prove, usually with an exchange argument, that the choice never rules out an optimal solution.
Build a candidate one choice at a time, recurse, then undo the choice, abandoning any partial candidate that can't lead to a valid answer: the way to enumerate subsets, permutations and constraint puzzles.
A tree where each edge is a character and each path from the root spells a prefix, so insert, lookup and "any word starting with...?" cost O(key length), however many words are stored.
In a "design a class" round no single structure makes every operation O(1), so you compose two: a hash map to find things plus a list, linked list or stack that keeps the order.