Pattern Recognition Guide
Learn to identify the right approach for any coding problem
The Golden Rule: Check Constraints First
Before anything else, look at the input size. This tells you which algorithms are even possible.
n ≤ 20
Brute force OK
O(2ⁿ), O(n!)
n ≤ 3000
Nested loops OK
O(n²)
n ≤ 10⁶
Single loop
O(n), O(n log n)
n > 10⁶
Math/Binary Search
O(log n), O(1)
Quick Pattern Lookup
If you see this input...
Sorted array→Binary Search, Two Pointers
Unsorted array→HashMap, Sort first, Sliding Window
String→Two Pointers, Sliding Window, Stack
Tree→DFS, BFS, Recursion
Graph→BFS, DFS, Union Find
2D Grid→DFS/BFS, DP
Linked List→Two Pointers (fast/slow)
Intervals→Sort + Greedy
If you need this output...
All combinations/subsets→Backtracking
Max/Min value→DP or Greedy
True/False (can reach?)→DP, BFS, or DFS
Shortest path→BFS (unweighted)
Number of ways→DP
Kth largest/smallest→Heap or QuickSelect
Simple Decision Flow
1
Check n→ Eliminates impossible approaches
2
Look at input type→ Array? Tree? Graph? String?
3
Check what output is needed→ Single number? List? Boolean?
4
Spot keywords→ "Shortest", "All combinations", "Maximum"