All cheat sheets
🧩
DSA Patterns
Pattern cheatsheet — recognize the problem shape faster.
Core Patterns
| Two pointers | Sorted arrays, pairs, reverse, containers — O(n) scans. |
|---|---|
| Sliding window | Subarrays/substrings with constraint (max sum, unique chars). |
| Hash map | Frequency, complement (Two Sum), first unique, anagrams. |
| Binary search | Sorted search, answer-space search (min capacity, etc.). |
Trees & Graphs
| BFS | Level order, shortest path in unweighted graphs. |
|---|---|
| DFS | Path existence, components, topo prep, backtracking trees. |
| Topo sort | DAG ordering — Kahn's (BFS) or DFS finish times. |
| Union-Find | Connected components, cycle detection in undirected graphs. |
DP Triggers
| Optimal substructure | Solution builds from smaller subproblems. |
|---|---|
| Overlapping subs | Same subproblems repeat → memo / tabulation. |
| Classic DPs | Knapsack, LCS/LIS, coin change, grid paths, house robber. |
| State design | Define dp[i] / dp[i][j] meaning first — then transitions. |