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.

Two Pointer

When you see a sorted array or a linked list problem with pair/triplet semantics, two pointers is almost always the right tool.

7 classic problems
Sliding Window

Substring and subarray problems with a contiguous-range constraint collapse to O(n) via two pointers that only expand.

7 classic problems
Binary Search

Not just for sorted arrays. Binary search the answer space when the predicate is monotonic and the bounds are integer.

7 classic problems
Monotonic Stack

Next-greater, next-smaller, and span problems are all the same pattern with the same O(n) stack invariant.

7 classic problems
Fast and Slow Pointer

Floyd's tortoise and hare: detect cycles, find middle, and reverse-with-finds in O(1) extra space on linked lists.

7 classic problems
Merge Intervals

Any 'overlapping ranges' problem sorts by start, then sweeps left-to-right with a single accumulator.

7 classic problems
Topological Sort

Dependency ordering on a DAG: Kahn's BFS or DFS post-order, both O(V+E).

7 classic problems
Union Find

Connected components, redundancy detection, and Kruskal's MST all reduce to find + union with path compression.

7 classic problems
Backtracking

Permutations, combinations, partitions, and constraint-satisfaction problems share the same recursive shape.

9 classic problems
Dynamic Programming

DP is recognition: identify the state, the transition, and the order. The rest is implementation.

9 classic problems
Graph BFS

Shortest path in unweighted graphs, level-order traversal, and multi-source spreading all use the same queue loop.

7 classic problems
Graph DFS

Cycle detection, connected components, and most tree-recursion variants use DFS with three states.

7 classic problems
Prefix Sum

Range sum queries collapse from O(n) per query to O(1) with one precomputation pass.

7 classic problems
Heap / Top K Elements

K-th largest, K closest points, and merge-K-streams problems all collapse to a size-K heap.

7 classic problems

Related: System design problems · All DP questions · All interview guides