Cheatsheets
Revise at a glance
Complexity tables, pattern recognition guides and battle-tested code templates — everything you skim the night before the interview.
Sorting & Searching complexities
n = input size, k = value range, d = digits.
| Algorithm | Best | Average | Worst | Space | Stable |
|---|---|---|---|---|---|
| Bubble Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Selection Sort | O(n²) | O(n²) | O(n²) | O(1) | No |
| Insertion Sort | O(n) | O(n²) | O(n²) | O(1) | Yes |
| Merge Sort | O(n log n) | O(n log n) | O(n log n) | O(n) | Yes |
| Quick Sort | O(n log n) | O(n log n) | O(n²) | O(log n) | No |
| Heap Sort | O(n log n) | O(n log n) | O(n log n) | O(1) | No |
| Counting Sort | O(n+k) | O(n+k) | O(n+k) | O(n+k) | Yes |
| Radix Sort | O(nd) | O(nd) | O(nd) | O(n+k) | Yes |
| Binary Search | O(1) | O(log n) | O(log n) | O(1) | — |
| Linear Search | O(1) | O(n) | O(n) | O(1) | — |
Data structure operations
Average-case costs. MC = min/max query.
| Structure | Access | Search | Insert | Delete | Notes |
|---|---|---|---|---|---|
| Array | O(1) | O(n) | O(n) | O(n) | Contiguous memory |
| Dynamic Array | O(1) | O(n) | O(1)* | O(n) | *amortized push |
| Linked List | O(n) | O(n) | O(1) | O(1) | O(1) at known node |
| Hash Table | — | O(1) | O(1) | O(1) | Worst O(n) collisions |
| BST (balanced) | O(log n) | O(log n) | O(log n) | O(log n) | O(n) if unbalanced |
| Heap | O(1) peek | O(n) | O(log n) | O(log n) | Min/max only |
| Trie | — | O(L) | O(L) | O(L) | L = key length |
| Segment Tree | — | O(log n) | O(log n) | O(log n) | Range queries |
| Fenwick (BIT) | — | O(log n) | O(log n) | — | Prefix sums |
| Union-Find | — | O(α(n)) | O(α(n)) | — | α ≈ constant |
Graph algorithms
V = vertices, E = edges.
| Algorithm | Time | Space | Use when |
|---|---|---|---|
| BFS | O(V+E) | O(V) | Shortest unweighted path, level order |
| DFS | O(V+E) | O(V) | Components, cycles, topological order |
| Topological Sort | O(V+E) | O(V) | Ordering DAG tasks / prerequisites |
| Union-Find | O(α(n)) | O(V) | Dynamic connectivity, cycle detection |
| Dijkstra | O(E log V) | O(V) | Shortest path, non-negative weights |
| Bellman-Ford | O(V·E) | O(V) | Negative weights, negative cycles |
| Floyd-Warshall | O(V³) | O(V²) | All-pairs shortest paths |
| Kruskal MST | O(E log E) | O(V) | MST via edge sorting + union-find |
| Prim MST | O(E log V) | O(V) | MST via priority queue |
| Kahn's Toposort | O(V+E) | O(V) | Topological order via in-degrees |
Classic DP problems
Recognize the state, the transition and the base case.
| Problem | State | Transition | Time |
|---|---|---|---|
| Climbing Stairs | ways[i] | ways[i-1] + ways[i-2] | O(n) |
| House Robber | rob[i] | max(rob[i-1], rob[i-2] + v[i]) | O(n) |
| Coin Change | min[v] | min(min[v-c] + 1) over coins | O(n·k) |
| 0/1 Knapsack | dp[i][w] | max(skip, take + dp[i-1][w-wt]) | O(n·W) |
| LCS | dp[i][j] | match ? 1+dp[i-1][j-1] : max(left, up) | O(n·m) |
| Edit Distance | dp[i][j] | min(insert, delete, replace) | O(n·m) |
| LIS | len[i] | max over j<i where a[j]<a[i] | O(n²) / O(n log n) |
| Unique Paths | dp[i][j] | dp[i-1][j] + dp[i][j-1] | O(n·m) |
| Partition Subsets | reach[s] | reach[s] || reach[s - v[i]] | O(n·S) |
Pattern recognition guide
What the problem statement is telling you.
| If the problem says… | Think… |
|---|---|
| Sorted array | Binary search or two pointers |
| Top / smallest K elements | Heap of size k |
| All combinations / permutations | Backtracking |
| Contiguous subarray or substring | Sliding window or prefix sums |
| Count ways to do X | Dynamic programming |
| Min/max cost to reach a goal | DP or Dijkstra (on states) |
| Connected groups / components | BFS/DFS or Union-Find |
| Dependencies / ordering | Topological sort |
| Pairs that satisfy a condition | Hash map or sort + two pointers |
| Overlapping ranges | Sort by start, sweep with heap |
| Fast lookup of seen items | Hash set / hash map |
| Linked list middle / cycle | Fast & slow pointers |
| Prefix of strings | Trie |
| Range sum / range min queries | Prefix sums / segment tree |
| Does path exist with constraint | BFS/DFS over (state) graph |
Code templates
The implementations you should be able to write from memory. Click to expand.