Data structures & algorithms
Learn the pattern,
not 500 problems.
Most interview problems are one of about eighteen patterns in disguise. Learn to recognise them, watch each one run step by step, then practise on a curated ladder.
- 01
Get the idea
An everyday analogy and the one insight that makes the pattern fast.
- 02
Watch it run
Step through the algorithm line by line, or type your own input.
- 03
Solve the ladder
6–8 LeetCode problems, easy to hard, with hints that don't spoil it.
- 04
Get grilled
Run a mock interview on the pattern and get an honest score.
Roadmap
Ordered by prerequisite.
Go top to bottom the first time. Each pattern leans on the ones before it.
Arrays & Strings
Two Pointers
Two indices squeeze inward so every pair never gets checked.
spot it: sorted array · find a pair (or triplet) that sums to target
Sliding Window
Grow the right edge, shrink the left, never restart.
spot it: contiguous subarray / substring · longest / shortest / maximum window such that…
Prefix Sum + Hash Map
Any range sum is a difference of two running totals.
spot it: subarray sum equals k · count the number of subarrays
Binary Search
Halve the search space every step — on arrays or answers.
spot it: sorted array / rotated sorted array · find the first / last position of
Monotonic Stack
Unanswered items wait on a stack until someone bigger arrives.
spot it: next greater / next smaller element · how many days until a warmer temperature
Merge Intervals
Sort by start, then only compare with the last one.
spot it: list of intervals [start, end] · merge overlapping
Greedy
Make the locally best choice and never look back.
spot it: can you reach the end · minimum number of jumps / steps
Linked Lists
Fast & Slow Pointers
A runner twice as fast laps you only if there's a loop.
spot it: linked list has a cycle · find where the cycle begins
In-place Reversal
Flip one arrow at a time with prev, curr, next.
spot it: reverse the linked list · reverse between positions left and right
Trees
Tree BFS (Level Order)
Process the tree floor by floor using a queue.
spot it: level order / level by level · right side view / left view
Tree DFS (Recursion)
Trust children to answer; combine their answers on the way up.
spot it: maximum depth / height · diameter / longest path
Graphs
Graph BFS / DFS (Grids)
Flood-fill each unseen region once; count the floods.
spot it: 2-D grid of '1's and '0's · number of islands / connected regions
Topological Sort
Do whatever has no blockers left, then unblock the rest.
spot it: prerequisites / dependencies · can you finish all courses
Union-Find (DSU)
Every group elects a leader; merging means one leader bows.
spot it: number of connected components / provinces · edges arrive one by one (dynamic connectivity)
Recursion & DP
Backtracking
Choose, explore, un-choose — walk every branch of the decision tree.
spot it: return all possible subsets / combinations / permutations · generate all valid …
1-D Dynamic Programming
Best answer here = best of a few earlier answers.
spot it: maximum / minimum total with a restriction · number of ways to reach / decode
2-D Dynamic Programming
A grid of subproblems, each filled from its neighbours.
spot it: two strings / two sequences · longest common subsequence
Cheatsheet
“Which pattern is this?”
Read the problem, find the phrase, jump to the pattern. The fastest way to stop staring at a blank editor.
| If the problem says… | Reach for |
|---|---|
| sorted array · find a pair (or triplet) that sums to target · in-place, O(1) extra space | Two Pointers |
| contiguous subarray / substring · longest / shortest / maximum window such that… · at most k distinct characters | Sliding Window |
| subarray sum equals k · count the number of subarrays · contains negative numbers | Prefix Sum + Hash Map |
| sorted array / rotated sorted array · find the first / last position of · minimum capacity / speed / time such that… | Binary Search |
| next greater / next smaller element · how many days until a warmer temperature · previous less element | Monotonic Stack |
| list of intervals [start, end] · merge overlapping · insert a new interval | Merge Intervals |
| can you reach the end · minimum number of jumps / steps · maximize profit with unlimited transactions | Greedy |
| linked list has a cycle · find where the cycle begins · middle of the linked list | Fast & Slow Pointers |
| reverse the linked list · reverse between positions left and right · reverse in groups of k | In-place Reversal |
| kth largest / kth smallest · top k frequent · k closest points | Heap / Top-K |
| level order / level by level · right side view / left view · minimum depth / shortest path in a tree | Tree BFS (Level Order) |
| maximum depth / height · diameter / longest path · path sum from root to leaf | Tree DFS (Recursion) |
| 2-D grid of '1's and '0's · number of islands / connected regions · shortest path in a maze / unweighted graph | Graph BFS / DFS (Grids) |
| prerequisites / dependencies · can you finish all courses · valid build / task order | Topological Sort |
| number of connected components / provinces · edges arrive one by one (dynamic connectivity) · redundant connection / creates a cycle | Union-Find (DSU) |
| return all possible subsets / combinations / permutations · generate all valid … · each number may be used once / unlimited times | Backtracking |
| maximum / minimum total with a restriction · number of ways to reach / decode · cannot pick adjacent elements | 1-D Dynamic Programming |
| two strings / two sequences · longest common subsequence · minimum edits to convert | 2-D Dynamic Programming |