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

  1. What is the input? (list, tree, intervals, grid or graph, words, or an array or string)
  2. What exactly do they ask for? (all of them, how many, best, k-th, contiguous, a position…)
  3. 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"]
Start with the shape of the input. Lists, trees, intervals, graphs and word sets almost choose the pattern for you.

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"]
For arrays and strings, the wording of the question is the clue. Match the phrase, then confirm with the pattern's 3-second gut check.

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

PatternOften confused with
Arrays & HashingTwo Pointers, Sliding Window, Heap / Priority Queue
Two PointersSliding Window, Binary Search, Arrays & Hashing
Sliding WindowTwo Pointers, Arrays & Hashing, 1-D Dynamic Programming
StackTwo Pointers, Heap / Priority Queue, Greedy
Binary SearchTwo Pointers, Heap / Priority Queue, Greedy
Linked ListTwo Pointers, Arrays & Hashing, Stack
TreesGraphs, Backtracking, Binary Search
Heap / Priority QueueBinary Search, Arrays & Hashing, Greedy
Backtracking1-D Dynamic Programming, Graphs, Trees
TriesArrays & Hashing, Backtracking, Trees
GraphsTrees, Advanced Graphs, 2-D Dynamic Programming
Advanced GraphsGraphs, Heap / Priority Queue, 1-D Dynamic Programming
1-D Dynamic ProgrammingGreedy, Backtracking, 2-D Dynamic Programming
2-D Dynamic Programming1-D Dynamic Programming, Graphs, Backtracking
Greedy1-D Dynamic Programming, Sliding Window, Intervals
IntervalsGreedy, Heap / Priority Queue, Two Pointers
Math & GeometryArrays & Hashing, 2-D Dynamic Programming, Bit Manipulation
Bit ManipulationMath & Geometry, Arrays & Hashing, Greedy