DSA · Patterns
24 patterns, most of the problems
Interview problems repeat a small set of ideas. Spot the pattern, watch it run, then practise. New to this? Go in order: each one builds on the last.
Arrays & strings
- Pattern 1 · 25 min
Hash Maps and Sets
Trade memory for time: remember what you have seen so each lookup is O(1) instead of another scan.
17 problems - Pattern 2 · 30 min
Two Pointers
Walk two indices through an array, from both ends or at two speeds, so one pass replaces a nested loop.
10 problems - Pattern 3 · 30 min
Sliding Window
Keep a window over a contiguous range and update it incrementally as it slides, instead of recomputing every subarray.
8 problems - Pattern 4 · 25 min
Prefix Sums and Difference Arrays
Precompute running totals once, then answer any range-sum question by subtracting two numbers.
8 problems - Pattern 6 · 30 min
Stacks and Monotonic Stacks
Use a stack for anything nested or most-recent-first, and a monotonic stack to find the next greater or smaller element in O(n).
9 problems - Pattern 7 · 25 min
Intervals and Sweep Line
Sort intervals by start, then merge, insert or count overlaps in a single sweep.
8 problems - Pattern 24 · 35 min
Matrix and Grid Simulation
Rotate, spiral, zero out and simulate on a 2D grid using boundaries, direction arrays and in-place encodings.
9 problems
Search & optimisation
- Pattern 5 · 30 min
Binary Search
Halve the search space every step, on a sorted array or on the answer itself, for O(log n) decisions.
16 problems - Pattern 18 · 35 min
Backtracking
Build a solution one choice at a time, recurse, then undo the choice: the template behind subsets, permutations and puzzles.
7 problems - Pattern 19 · 25 min
Greedy
Make the best local choice at each step and prove it never hurts, usually after sorting.
9 problems - Pattern 20 · 40 min
Dynamic Programming I: 1D
Define what dp[i] means, write how it depends on smaller i, and fill a table instead of recomputing overlapping subproblems.
15 problems - Pattern 21 · 40 min
Dynamic Programming II: Grids, Strings and Knapsack
dp[i][j] over two strings, a grid, or items × capacity: the same recipe with one more dimension.
8 problems - Pattern 23 · 40 min
Design Data Structures: LRU, LFU and Friends
Combine a hash map with a list, an array or sorted buckets so that every operation of a class costs O(1) or O(log n).
7 problems
Linked lists
- Pattern 8 · 25 min
Fast and Slow Pointers
Move one pointer twice as fast as the other to detect cycles, find the middle, or find where a loop starts, in O(1) space.
7 problems - Pattern 9 · 30 min
In-place Linked List Manipulation
Reverse, merge and splice lists by rewiring next pointers with prev/curr/next, using a dummy head to remove edge cases.
7 problems
Trees
- Pattern 10 · 35 min
Tree Depth-First Search
Solve a tree problem by solving it for the left and right subtrees and combining the answers on the way back up.
15 problems - Pattern 11 · 25 min
Tree Breadth-First Search
Process a tree level by level with a queue, which makes level sums, views and minimum depths natural.
8 problems - Pattern 12 · 25 min
Tries (Prefix Trees)
Store strings character by character in a tree so prefix lookups cost O(length of word), not O(number of words).
5 problems - Pattern 13 · 35 min
Heaps: Top-K, K-way Merge and Two Heaps
A heap gives you the smallest (or largest) item in O(1) and updates in O(log n): perfect for top-k, merging k sorted lists and running medians.
9 problems
Graphs
- Pattern 14 · 35 min
Graph BFS and DFS
Explore grids and graphs with a visited set: DFS to flood regions, BFS for the fewest steps.
20 problems - Pattern 15 · 25 min
Topological Sort
Order the nodes of a directed graph so every edge points forward, and detect cycles when that is impossible.
5 problems - Pattern 16 · 25 min
Union-Find (Disjoint Set Union)
Track which items belong to the same group with near-constant-time union and find, using path compression and union by rank.
10 problems - Pattern 17 · 35 min
Shortest Paths: BFS, Dijkstra, Bellman-Ford
BFS for unweighted graphs, Dijkstra with a heap for non-negative weights, and Bellman-Ford when the number of edges is limited.
5 problems