Coding Interviews
The fastest way to get good at coding interviews isn't memorizing 500 problems — it's recognizing the ~12 recurring patterns underneath them. Each pattern below lists the signals that tell you it applies, common pitfalls, and worked example problems with full solutions.
How to recognize a pattern
Most problems telegraph their pattern through their constraints and the shape of what they ask for, before you write a single line of code. A subarray/substring with a length or sum condition usually means Sliding Window. A sorted array with a target-pair or target-sum condition usually means Two Pointers. "Top/smallest K" asks signal a Heap. Repeated overlapping subproblems on a small state space signal Dynamic Programming. Graph or grid connectivity and shortest-path phrasing signal BFS/DFS or Union-Find. Train yourself to read the constraints (input size, whether it's sorted, what's being optimized) as clues, then confirm the pattern against the "signals" list on each pattern page below rather than guessing from the problem's surface story.
Two Pointers
3 problemsUse two indices moving through a (usually sorted) sequence — toward each other, or at different speeds — to avoid nested loops.
Sliding Window
2 problemsMaintain a window (contiguous subarray/substring) that expands and contracts, tracking a running property, to avoid recomputation.
Fast & Slow Pointers (Floyd's)
2 problemsTwo pointers moving at different speeds through a linked list or implicit sequence to detect cycles or find midpoints.
Merge Intervals
1 problemSort intervals by start time, then sweep once, merging overlapping ranges as you go.
Binary Search (incl. on answer space)
1 problemHalve the search space each step; applies not just to sorted arrays but to any monotonic predicate space.
BFS / DFS (Graph & Tree Traversal)
2 problemsBreadth-first search explores level by level (shortest path in unweighted graphs); depth-first explores one branch fully before backtracking.
Dynamic Programming
3 problemsBreak a problem into overlapping subproblems, solve each once, and cache the result (memoization or tabulation).
Backtracking
1 problemExplore all candidate solutions incrementally, abandoning ('pruning') a branch as soon as it can't lead to a valid solution.
Heaps / Priority Queues (Top-K)
4 problemsMaintain a min/max-heap of a bounded size to efficiently track the K largest/smallest elements without sorting everything.
Union-Find (Disjoint Set)
0 problemsA data structure that tracks a partition of elements into disjoint sets, supporting near-O(1) union and find via path compression + union by rank.
Topological Sort
1 problemOrder nodes of a DAG such that every directed edge u→v places u before v — via BFS (Kahn's algorithm, in-degree counting) or DFS (post-order reversal).
Trie (Prefix Tree)
2 problemsA tree where each path from root represents a string prefix, enabling fast prefix search/autocomplete.
Greedy Algorithms
2 problemsMake the locally optimal choice at each step, trusting that this leads to a global optimum — only works when the problem has the greedy-choice property.
Bit Manipulation
2 problemsUse bitwise operators (&, |, ^, <<, >>) to solve problems at the bit level, often achieving O(1) space and clever constant-factor speedups.
Matrix / Grid Traversal
2 problemsNavigate a 2D grid in a specific order (spiral, diagonal, rotation, layer-by-layer) or apply in-place transformations.
Monotonic Stack
2 problemsMaintain a stack that is always increasing or decreasing, popping elements that violate the invariant — enables O(n) solutions for 'next greater/smaller element' problems.
Prefix Sum / Difference Array
2 problemsPrecompute cumulative sums (prefix[i] = sum of elements 0..i) to answer range-sum queries in O(1), or use a difference array for range updates.
Design Data Structures
2 problemsImplement a custom data structure (LRU cache, LFU cache, min stack, range queries) meeting specific API and time-complexity requirements.