01Arrays & Hashing
258 problemsTrade memory for time with sets, maps and counting.
Reach for it when: You need to look something up you have already seen, count frequencies, or group by a key.
Eighteen patterns cover almost every interview problem. Recognising which one a problem needs is most of the work, so each pattern below comes with the cue in a problem statement that should make you reach for it. They are listed in roadmap order: each builds on the ones before it.
Trade memory for time with sets, maps and counting.
Reach for it when: You need to look something up you have already seen, count frequencies, or group by a key.
Walk inward or in lockstep over a sorted or symmetric sequence.
Reach for it when: The input is sorted (or can be), or you compare elements from both ends.
Builds on Arrays & Hashing
Grow and shrink a contiguous range while keeping an invariant.
Reach for it when: Longest, shortest or count of a contiguous subarray or substring with a constraint.
Builds on Two Pointers
Defer work until a matching or smaller element appears.
Reach for it when: Matching brackets, nested structure, or the next greater / smaller element.
Builds on Arrays & Hashing
Halve a monotonic search space, on indices or on the answer.
Reach for it when: Sorted input, rotated arrays, or "minimum value that works" with a monotonic check.
Builds on Two Pointers
Pointer surgery with dummy heads and fast / slow runners.
Reach for it when: Reversing, merging, reordering nodes, or detecting a cycle in place.
Builds on Two Pointers
Recursive DFS and level-order BFS over hierarchical data.
Reach for it when: A binary tree or BST: depth, paths, ancestors, or level-by-level processing.
Builds on Binary Search, Linked List
Prefix trees for fast word and prefix lookups.
Reach for it when: Many words with shared prefixes, autocomplete, or wildcard word search.
Builds on Trees
Always pull the current best element in log time.
Reach for it when: Top K, K closest, running median, or repeatedly taking the smallest / largest.
Builds on Trees
Build candidates step by step and undo choices that fail.
Reach for it when: Generate all subsets, permutations, combinations or board placements.
Builds on Trees
BFS, DFS, union find and topological order over nodes and edges.
Reach for it when: Grids of cells, connectivity, islands, dependencies or prerequisites.
Builds on Trees, Backtracking
Shortest paths, spanning trees and ordering with weights.
Reach for it when: Weighted edges, cheapest / fastest path, or connecting everything at minimum cost.
Builds on Graphs, Heap / Priority Queue
Overlapping subproblems indexed by a single position.
Reach for it when: Count ways, min / max over choices at each index, and greedy fails a counterexample.
Builds on Backtracking
State over two indices: grids, two strings, or item and capacity.
Reach for it when: Compare two sequences, walk a grid, or choose items under a budget.
Builds on 1-D Dynamic Programming, Graphs
Commit to the locally best choice when it provably stays optimal.
Reach for it when: A single pass where the best local move never needs to be undone.
Builds on Arrays & Hashing, 1-D Dynamic Programming
Sort by start or end, then merge or sweep.
Reach for it when: Ranges with starts and ends: overlaps, merging, meeting rooms.
Builds on Arrays & Hashing, Heap / Priority Queue
Matrix manipulation, number tricks and coordinate reasoning.
Reach for it when: Rotate or spiral a matrix, digit manipulation, or points on a plane.
Builds on Arrays & Hashing
XOR, masks and shifts on the binary representation.
Reach for it when: Find the odd one out, count bits, or arithmetic without operators.
Builds on Arrays & Hashing