Sliding Window
Turn nested scans of subarrays and substrings into one linear pass by maintaining a window that grows and shrinks as you go.
The pattern library
Most interview questions are variations on a small set of patterns. Learn each one, then let your daily reviews keep it in memory.
Turn nested scans of subarrays and substrings into one linear pass by maintaining a window that grows and shrinks as you go.
Walk a sequence from both ends (or at two speeds) to find pairs and partitions without a second nested loop.
Detect cycles and find midpoints in linked structures with two pointers moving at different speeds.
Halve the search space every step - on sorted arrays, but also on answer ranges you can test monotonically.
Explore level by level with a queue - the go-to for shortest paths in unweighted graphs and grids.
Follow each branch to the end before backtracking - the backbone of tree traversal, connectivity, and exhaustive search.
Precompute running totals so any range query collapses to one subtraction.
Reverse, splice, and rewire lists in place with careful pointer bookkeeping and a dummy head.
Build candidates one choice at a time and undo dead ends - permutations, combinations, and constraint puzzles.
Break problems into overlapping subproblems and cache the answers, bottom-up or top-down.
Take the locally best choice at each step - and know how to argue it stays globally optimal.
Keep the K best items in a heap so you never sort more than you need.
Sort by start, then sweep: merging, inserting, and detecting overlaps in interval lists.
A stack that stays sorted answers 'next greater element' questions in one pass.
Track connected components with near-constant-time merges - cycles, islands, and network connectivity.
A prefix tree that makes word lookup, autocomplete, and prefix counting linear in word length.
Order tasks with dependencies using indegrees and a queue - and detect when no valid order exists.
XOR tricks, masks, and shifts that turn set logic into constant-time integer operations.
Spirals, diagonals, islands, and flood fill - moving through 2D grids without losing your place.
A max-heap and min-heap balanced against each other give you a running median in log time.