Most interview problems are one of a small number of shapes wearing a new story. Each page below names the shape, the words in a problem that point at it, and the neighbour it gets confused with.
Arrays
Hashing
Two pointers
Two pointers (opposite ends)
O(n)Two indices start at the ends of a sorted array and walk toward each other. Each step moves whichever side is holding the answer back.
Two pointers (same direction)
O(n)A read pointer scans ahead while a write pointer trails behind it. The write pointer moves only when a value is worth keeping.
Sliding window
Sliding window (fixed size)
O(n)A window of exactly k elements rolls one step at a time. Add the value coming in, drop the one going out, reuse the rest.
Sliding window (variable)
O(n)Grow the window from the right, and shrink it from the left when the condition breaks. Neither end ever moves backwards.
Prefix sums
Prefix sums
O(n) build / O(1) queryStore a running total of everything that comes before each index. A range sum then costs one subtraction instead of a loop.
Difference array
O(n)Record the change at each index instead of the value. A range update costs two writes, and one final pass rebuilds everything.
Searching
Linear search
O(n)Walk the collection from one end and stop at the first item that matches. No order, no structure and no setup are needed.
Binary search (array)
O(log n)Look at the middle of a sorted range and throw away the half that cannot hold the answer. Repeat until one item is left.
Binary search on a rotated array
O(log n)A sorted array that was cut and swapped still has one sorted half at every step. Find that half, then pick a side to keep.
Binary search on the answer
O(n log(range))The thing you search here is the range of every possible answer. A yes-or-no test decides which half of it to throw away.
Sorting
Elementary sorts (selection, bubble, insertion)
O(n^2)Selection, bubble and insertion sort all compare neighbours and swap them. All three cost O(n squared) on a shuffled array.
Fast sort (merge / quick)
O(n log n)Cut the array in two, sort each part, then put them back together. Merge sort cuts by position, quick sort cuts by value.
Non-comparison sort (counting / radix)
O(n)Counting and radix sort never compare two values. They read the key itself, which beats n log n when the range is small.
Sort with a custom comparator
O(n log n)A comparator answers exactly one question: which of these two items comes first. The sort algorithm handles everything else.
Intervals
Intervals: merge & insert
O(n log n)Sort the intervals by start, then walk them once. Two neighbours merge whenever the next start is not past the current end.
Sweep line (event counting)
O(n log n)Turn each interval into a start event and an end event. Then sort all the events by position and walk them with a counter.
Linked lists
Linked list reversal
O(n)Walk the list once, and at every node flip its next pointer to the node behind it. Three local variables are all you need.
Fast & slow pointers
O(n)One pointer moves a single step at a time while the other moves two steps. The gap between them exposes cycles and midpoints.
Linked list merge & reorder
O(n + m)Build the answer on a dummy node and take from the front of each list. That dummy removes every empty-list special case.
Stacks and queues
Stack (LIFO)
O(n)A stack always hands back the most recent item first. That makes it the tool for anything that has to close in reverse order.
Monotonic stack
O(n)Keep the stack's values increasing from bottom to top. Pop everything the new value beats, and each pop just found its answer.
Monotonic deque (sliding window max/min)
O(n)A deque kept in order, so its front is always the window's maximum. Beaten values leave the back, expired ones the front.
Recursion
Backtracking
Trees
Tree traversal
O(n)Visit every node of the tree exactly once. Where exactly you place the visit is what decides what the traversal computes.
Binary search tree
O(log n) avgA search tree keeps all the smaller values to the left and the larger ones to the right. Every lookup drops one whole side.
Lowest common ancestor
O(n)The lowest common ancestor is the deepest node that has both targets below it. One post-order pass over the tree finds it.
Tree serialize / deserialize
O(n)Write a tree out as one flat string, then rebuild it exactly. The markers for the missing children are what make it work.
Heaps and top-k
Graphs
Graph BFS / DFS
O(V+E)Walk a graph from a start node, marking everything you have seen. A queue gives the fewest hops, while a stack goes deep.
Connected components
O(V+E)Start a new walk at every node you have not seen yet. Every walk that actually starts marks out one more connected group.
Topological sort (Kahn's algorithm)
O(V+E)Order the tasks so that every one of them comes after everything it depends on. Keep taking whichever node owes nothing.
Union-find (disjoint set)
near O(1) per opEvery element points at a parent, and walking up reaches a root that names its group. Two elements match when their roots match.
Dijkstra's algorithm
O(E log V)Settle the closest node not yet settled, then relax all of its edges. A heap keeps that node always one single read away.
Eulerian path (Hierholzer's algorithm)
O(E)Walk a graph using every one of its edges exactly once. Push a node the moment it gets stuck, then read the answer backwards.
Bipartite check (two-coloring)
O(V+E)Colour any one node, then colour each of its neighbours in the other colour. A conflict proves the graph is not bipartite.
Greedy
Dynamic programming
Dynamic programming (1-D)
O(states)Answer a small version of the problem, store the result, then build the next one from that. Every state is computed once.
DP on subsequences (LIS / LCS / edit distance)
O(n^2)/O(nm)The state here is a pair of positions, one in each of the two sequences. Every cell asks whether those two items match up.
DP on intervals
O(n^3)The state is a range, and the transition picks the last move made inside it. The shorter ranges are always filled first.
DP on trees
O(n)Every node's answer is built out of the answers given by its own children. A single post-order pass computes all of them.
Strings and dynamic programming
Strings
Bit manipulation
Math and number theory
Matrix
Matrix traversal (spiral / diagonal)
O(mn)Walk a grid in an order that the plain nested loops do not give you. Bounds or a direction rule decide the cell after this one.
Matrix search (sorted grid)
O(m+n)In a grid sorted both ways, one corner can only move in a single direction. Each comparison drops a whole row or column.
Matrix word search (grid DFS/backtracking)
O(mn*4^L)Search a grid by stepping into a neighbour and then stepping back out again. The cell stays marked while you are inside it.