Patterns
Almost every coding problem is one of these 19 patterns in disguise. Learn to spot the clue, and you know where to start.
Hashing and counting
Remember what you have seen in a Map, so you never search for it twice.
Spot it when: You need to find a match, a duplicate, or count how often things appear.
Two pointers
Walk through a list from both ends (or at two speeds) at once.
Spot it when: The list is sorted, or you compare items from the start and the end.
Sliding window
Keep a stretch of the list and slide it along instead of starting over.
Spot it when: You want the best stretch of items next to each other.
Binary search
Halve the search space with every guess.
Spot it when: The data is sorted, or the answer is a number you can test 'too big or too small'.
Stack
Keep the most recent unfinished thing on top, and finish it first.
Spot it when: Things must match up in pairs, or you need the next bigger or smaller item.
Prefix sums
Add up running totals once, then answer any range sum instantly.
Spot it when: You are asked about sums of many different ranges.
Coming soon
Fast and slow pointers
Move one pointer twice as fast as the other to find loops and middles.
Spot it when: A linked list might loop back on itself, or you need its middle.
Coming soon
Linked list tricks
Rewire nodes in place: reverse, merge, and remove.
Spot it when: The input is a linked list and you must change its order.
Coming soon
Tree traversal
Visit every node of a tree, depth first or level by level.
Spot it when: The input is a tree, or the problem has a parent-child shape.
Coming soon
Heaps and top K
Always have the smallest (or biggest) item ready to take.
Spot it when: You need the K biggest, K smallest, or the next item by priority.
Coming soon
Intervals
Sort ranges by their start, then merge or compare neighbours.
Spot it when: The input is a list of start and end times.
Coming soon
Backtracking
Try a choice, explore everything after it, then undo it and try the next.
Spot it when: You must list every combination, arrangement or path.
Coming soon
Graph search
Explore connected things with breadth-first or depth-first search.
Spot it when: Things are connected to each other: grids, maps, networks.
Coming soon
Topological sort
Put tasks in an order where everything comes after what it depends on.
Spot it when: Some things must happen before others.
Coming soon
Union-find
Track which items belong to the same group as groups merge.
Spot it when: You keep joining items and asking whether two are connected.
Coming soon
Dynamic programming
Build the answer from answers to smaller versions of the problem, saved in a table.
Spot it when: You count ways or find the best total, and choices overlap.
Coming soon
Greedy
Take the best-looking choice at each step, when that is provably safe.
Spot it when: A simple local rule (earliest, cheapest, biggest) seems to always work.
Coming soon
Tries
Store words letter by letter so shared beginnings are stored once.
Spot it when: You search many words by their prefix.
Coming soon
Bit manipulation
Use the ones and zeros inside numbers to solve problems with tiny memory.
Spot it when: The problem mentions binary, or a trick with XOR would cancel pairs out.
Coming soon