Coding Interview Patterns
14 coding patterns that cover ~90% of the coding interview problems asked at top companies. Each page includes when to use the pattern, the template code, time and space complexity, classic problems to practice, and the common mistakes that lose offers.
When you see a sorted array or a linked list problem with pair/triplet semantics, two pointers is almost always the right tool.
Substring and subarray problems with a contiguous-range constraint collapse to O(n) via two pointers that only expand.
Not just for sorted arrays. Binary search the answer space when the predicate is monotonic and the bounds are integer.
Next-greater, next-smaller, and span problems are all the same pattern with the same O(n) stack invariant.
Floyd's tortoise and hare: detect cycles, find middle, and reverse-with-finds in O(1) extra space on linked lists.
Any 'overlapping ranges' problem sorts by start, then sweeps left-to-right with a single accumulator.
Dependency ordering on a DAG: Kahn's BFS or DFS post-order, both O(V+E).
Connected components, redundancy detection, and Kruskal's MST all reduce to find + union with path compression.
Permutations, combinations, partitions, and constraint-satisfaction problems share the same recursive shape.
DP is recognition: identify the state, the transition, and the order. The rest is implementation.
Shortest path in unweighted graphs, level-order traversal, and multi-source spreading all use the same queue loop.
Cycle detection, connected components, and most tree-recursion variants use DFS with three states.
Range sum queries collapse from O(n) per query to O(1) with one precomputation pass.
K-th largest, K closest points, and merge-K-streams problems all collapse to a size-K heap.
Related: System design problems · All DP questions · All interview guides