Home
Zarak Shah

Leetcode Patterns

Pattern recognition accelerates problem-solving. Most interview problems are variations of a small set of underlying patterns. Recognizing the pattern tells you which algorithm to reach for.

Decision flowchart for selecting the right algorithm pattern based on input type

Sorted Input

  • Apply binary search for efficient lookups
  • Use the two-pointer technique for pairs or segments

Unsorted Input

  • Dynamic programming for counting ways or optimization problems
  • Backtracking for all possibilities and combinations
  • Trie for prefix matching
  • Hash map / set for quick element lookup
  • Monotonic stack or sliding window for continuous max/min values

Graph or Tree Input

  • DFS for exploring all paths (when the shortest path is not required)
  • BFS for shortest path or fewest steps
  • For binary trees, DFS when targeting specific depths or levels

Linked List Input

  • Slow/fast pointers or prev / dummy pointers for specific list operations
·