DSA · recognising patterns
Which pattern? Spot it from the question
Most of a coding interview is choosing the right pattern. Ask three questions, follow the two finders, then practise on real problems until you can name the pattern in seconds.
Three questions to ask first
- What is the input? (list, tree, intervals, grid or graph, words, or an array or string)
- What exactly do they ask for? (all of them, how many, best, k-th, contiguous, a position…)
- What are the limits? (n ≤ 20 means exponential is fine; 10⁵ means O(n log n) or better; 'O(1) space' or 'O(log n)' rule out options)
Step 1 · Look at the input
flowchart TD
I{"What is the input?"}
I -->|"a linked list"| LL["Linked List<br/>dummy node · prev/curr/next · fast & slow"]
I -->|"a tree"| T{"levels or 'closest'?"}
T -->|"yes"| TB["Trees: BFS with a queue"]
T -->|"no"| TD2["Trees: DFS recursion"]
I -->|"[start, end] pairs"| IV["Intervals: sort by start"]
I -->|"a grid, or nodes and edges"| G{"edges with different costs?"}
G -->|"yes"| AG["Advanced Graphs: Dijkstra"]
G -->|"no"| GR["Graphs: DFS/BFS · topological sort · union-find"]
I -->|"many words, prefixes"| TR["Tries"]
I -->|"an array or a string"| NEXT["→ Step 2: look at the question"]Step 2 · Arrays and strings: look at the question
flowchart TD
Q{"What are they asking for?"}
Q -->|"every combination / permutation"| BT["Backtracking"]
Q -->|"number of ways, or min / max with choices"| DP{"greedy breaks on a small example?"}
DP -->|"yes"| DPY["Dynamic Programming"]
DP -->|"no, one safe best move"| GRD["Greedy"]
Q -->|"best contiguous subarray / substring"| SW["Sliding Window"]
Q -->|"a pair or triplet, sorted data"| TP["Two Pointers"]
Q -->|"a position or smallest value that works, O(log n)"| BS["Binary Search"]
Q -->|"k-th, top k, a stream"| HP["Heap"]
Q -->|"next greater, matching brackets"| ST["Stack"]
Q -->|"seen before, counts, groups"| HM["Arrays & Hashing"]
Q -->|"pairs cancel, single bits, no + allowed"| BIT["Bit Manipulation"]
Q -->|"rotate, spiral, digits"| MG["Math & Geometry"]The drill
Read what a problem asks and pick its pattern. A right answer brings the card back later (1, 3, 7, 14, then 30 days); a wrong one brings it back tomorrow. Each day: the cards that are due, plus five new ones.
Loading the drill…
Patterns people confuse
| Pattern | Often confused with |
|---|---|
| Arrays & Hashing | Two Pointers, Sliding Window, Heap / Priority Queue |
| Two Pointers | Sliding Window, Binary Search, Arrays & Hashing |
| Sliding Window | Two Pointers, Arrays & Hashing, 1-D Dynamic Programming |
| Stack | Two Pointers, Heap / Priority Queue, Greedy |
| Binary Search | Two Pointers, Heap / Priority Queue, Greedy |
| Linked List | Two Pointers, Arrays & Hashing, Stack |
| Trees | Graphs, Backtracking, Binary Search |
| Heap / Priority Queue | Binary Search, Arrays & Hashing, Greedy |
| Backtracking | 1-D Dynamic Programming, Graphs, Trees |
| Tries | Arrays & Hashing, Backtracking, Trees |
| Graphs | Trees, Advanced Graphs, 2-D Dynamic Programming |
| Advanced Graphs | Graphs, Heap / Priority Queue, 1-D Dynamic Programming |
| 1-D Dynamic Programming | Greedy, Backtracking, 2-D Dynamic Programming |
| 2-D Dynamic Programming | 1-D Dynamic Programming, Graphs, Backtracking |
| Greedy | 1-D Dynamic Programming, Sliding Window, Intervals |
| Intervals | Greedy, Heap / Priority Queue, Two Pointers |
| Math & Geometry | Arrays & Hashing, 2-D Dynamic Programming, Bit Manipulation |
| Bit Manipulation | Math & Geometry, Arrays & Hashing, Greedy |