{}DSA Atlas

Pattern Recognition Guide

The decision table that maps problem signals and constraints to the right technique — the single most useful page for solving problems you've never seen.

The core idea

Most interview problems are variations of about 25 patterns. Solving an unseen problem is largely recognizing which pattern it is. The recognition comes from three sources:

  1. The input shape (sorted array, tree, grid, string pairs, intervals…)
  2. The question's keywords (“longest”, “all combinations”, “minimum steps”, “k-th”…)
  3. The constraints (n ≤ 20 vs n ≤ 10⁵ vs n ≤ 10⁹)

Test yourself with the pattern quiz.

Step 1: Read the constraints

n up toTarget complexitySuggests
10–12O(n!)permutations, brute force
20–25O(2ⁿ · n)subsets, bitmask DP, meet in the middle (n ≤ 40)
100–500O(n³)interval DP, Floyd-Warshall
1,000–5,000O(n²)2D DP, all pairs
10⁵–10⁶O(n log n) or O(n)sorting, heaps, binary search, two pointers, hashing, prefix sums, greedy
10⁹–10¹⁸O(log n) or O(1)binary search on answer, math, matrix exponentiation

Step 2: Match the signals

Arrays and strings

If the problem says…TryTopic
pair/triplet with a sum, sorted inputtwo pointersTwo Pointers
pair with a sum, unsorted, need indiceshash map complementHashing
longest/shortest contiguous subarray/substring with a conditionsliding windowSliding Window
subarray sum equals k (negatives allowed)prefix sum + hash mapPrefix Sums
many range-sum queries, no updatesprefix sumsPrefix Sums
many range updates, then readdifference arrayPrefix Sums
range queries with updatesFenwick / segment treeRange Queries
next greater/smaller element, spans, histogrammonotonic stackStacks
max/min of every windowmonotonic dequeStacks
k largest / smallest / most frequentheap (or quickselect)Heaps
in-place, O(1) extra space, values in 1..nindex as hash / cyclic sortHashing
anagram, frequency, duplicatescounting array / hash mapHashing
“find the element” in O(log n)binary searchBinary Search
minimum X such that feasible(X) / minimize the maximumbinary search on answerBinary Search
every element twice except one; subsets of ≤ 20 itemsbit manipulationBits
pattern occurrence, periodicityKMP / Z / hashingStrings

Linked lists

SignalTry
cycle, middle, k-th from endfast & slow pointers
reverse (all / part / in groups)three-pointer reversal
head may changedummy node
merge sorted listsmerge routine; k lists → heap

Trees

SignalTry
level by level, minimum depth, right viewBFS
height, diameter, balanced, path sumsDFS returning values (post-order)
constraints passed from ancestors (BST validity, max on path)DFS with parameters (pre-order)
BST → sorted order, k-th smallestin-order traversal
lowest common ancestorrecursive LCA (or BST walk)
choose nodes with neighbour constraintstree DP returning multiple states

Graphs and grids

SignalTryTopic
connected regions, islands, flood fillDFS/BFSGraphs
shortest path, unweighted / minimum stepsBFSGraphs
distance to nearest of many sourcesmulti-source BFSGraphs
dependencies, prerequisites, orderingtopological sortGraphs
shortest path, weighted non-negativeDijkstraShortest Paths
at most k edges / negative weightsBellman-FordShortest Paths
all pairs, n ≤ 400Floyd-WarshallShortest Paths
grouping by connectivity, edges added over timeUnion-FindUnion-Find
connect everything with minimum costMST (Kruskal/Prim)Advanced Graphs
critical edges/nodesTarjan bridges / articulation pointsAdvanced Graphs
can it be split into two groups with no conflictsbipartite checkGraphs

Search over choices

SignalTryTopic
return all combinations/permutations/partitionsbacktrackingBacktracking
count ways / min/max value with overlapping choicesdynamic programmingDP
locally best choice provably works (scheduling, jumps)greedyGreedy
two sequences compared/aligned2D DP on prefixes2D DP
subset with a target sum / capacityknapsackKnapsack
merging/removing adjacent elements with costsinterval DPAdvanced DP
n ≤ 20, visit/assign allbitmask DPAdvanced DP
count numbers ≤ N with a digit propertydigit DPAdvanced DP

Intervals

SignalTry
merge overlappingsort by start, sweep
max non-overlapping / min removals / arrowssort by end, greedy
min rooms / max concurrencysweep line events or min-heap of end times
insert into sorted intervals onlinebalanced BST / sorted map

Design

SignalTry
O(1) get/put with recency evictionhash map + doubly linked list (LRU)
O(1) insert/delete/randomarray + value→index map, swap-with-last
values over time / snapshotsper-key list + binary search
prefix lookups / autocompletetrie
running mediantwo heaps

Step 3: Keywords cheat sheet

KeywordUsually means
“contiguous”, “subarray”, “substring”sliding window, prefix sums, Kadane
“subsequence”DP (LIS, LCS) or two pointers (is-subsequence)
“all possible”, “generate”, “every combination”backtracking
“number of ways”DP (or combinatorics)
“minimum number of steps/moves”BFS (unweighted) or DP
“minimum cost path”Dijkstra or grid DP
“k-th largest/smallest”, “top k”heap, quickselect, binary search on value
“sorted” / “rotated sorted”binary search, two pointers
“in-place”, “O(1) space”two pointers, index marking, bit tricks
“stream”, “online”heaps, running aggregates, union-find
“prefix”, “dictionary of words”trie
“parentheses”, “nested”stack
“next greater”, “previous smaller”monotonic stack
“overlapping”, “meetings”, “schedule”intervals: sort + sweep
“connected”, “groups”, “provinces”union-find / DFS
“prerequisites”, “order”topological sort
“maximize the minimum” / “minimize the maximum”binary search on answer
“palindrome”two pointers, expand around center, DP
“XOR”bit tricks, prefix XOR, binary trie
“modulo 10⁹ + 7”counting DP or combinatorics

Step 4: When you're stuck

  1. Brute force first. Write down the naive solution and its complexity. Then ask what's repeated or wasted.
  2. What would sorting give me? Many problems unlock after sorting.
  3. What would a hash map give me? Replace an inner search with O(1) lookups.
  4. Can I binary search the answer? Is there a monotone yes/no question?
  5. Draw small examples. Trees, grids, and DP tables are much easier on paper.
  6. Think about the last step. What was the last choice made? (DP, interval DP, greedy exchange.)
  7. Reverse the problem. Work backwards from the target, reverse edges, add instead of delete (offline union-find).
  8. Change the representation. Grid → graph, string → counts, numbers → bits, intervals → events.
  9. Relax a constraint to find the core difficulty, then add it back.