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 arrayBinary Search, Two Pointers
Unsorted arrayHashMap, Sort first, Sliding Window
StringTwo Pointers, Sliding Window, Stack
TreeDFS, BFS, Recursion
GraphBFS, DFS, Union Find
2D GridDFS/BFS, DP
Linked ListTwo Pointers (fast/slow)
IntervalsSort + Greedy

If you need this output...

All combinations/subsetsBacktracking
Max/Min valueDP or Greedy
True/False (can reach?)DP, BFS, or DFS
Shortest pathBFS (unweighted)
Number of waysDP
Kth largest/smallestHeap 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"