Pattern Cheat Sheet

When you see these "smells" in a problem, reach for these patterns.

Hash Map / Set

Problem Smells

  • Need to find a complement or pair
  • Check for duplicates
  • Count frequencies or occurrences
  • Need O(1) lookups by value

Key Insight

Trade space for time. Store values you've seen for instant lookup.

Two Pointers

Problem Smells

  • Sorted array or linked list
  • Find a pair meeting a condition
  • Remove duplicates in-place
  • Partition or rearrange elements

Key Insight

Use two indices moving toward each other (or same direction) to avoid nested loops.

Sliding Window

Problem Smells

  • Contiguous subarray or substring
  • Longest/shortest with a constraint
  • Fixed-size window aggregation

Key Insight

Maintain a window [left, right] and expand/contract to satisfy constraints.

Binary Search

Problem Smells

  • Sorted input
  • Find minimum/maximum that satisfies a condition
  • Search on answer (monotonic predicate)

Key Insight

Halve the search space each step. Works on any monotonic function, not just arrays.

Stack

Problem Smells

  • Matching pairs (brackets, tags)
  • Next greater/smaller element
  • Nested or recursive structure to flatten

Key Insight

LIFO order naturally handles nesting. In Go, use a slice as a stack.

Linked List Manipulation

Problem Smells

  • Reverse order without extra space
  • Find middle or cycle
  • Merge sorted sequences in-place

Key Insight

Think in pointer rewiring. Use dummy nodes, slow/fast pointers, and prev/curr/next.

Tree Recursion

Problem Smells

  • Hierarchical or nested data
  • Need to aggregate bottom-up
  • Validate structure (BST property)

Key Insight

Most tree problems are: what do I compute at this node given results from children?

Examples

Graph Search (BFS/DFS)

Problem Smells

  • Grid or adjacency structure
  • Connected components or regions
  • Shortest path (unweighted)

Key Insight

DFS for reachability and exhaustive search. BFS for shortest path in unweighted graphs.

Dynamic Programming

Problem Smells

  • How many ways to reach state X?
  • Minimum cost / maximum value
  • Choices at each step with overlapping subproblems

Key Insight

Define the state, write the recurrence, optimize space. Start with brute force recursion, add memoization.

Heap / Priority Queue

Problem Smells

  • K largest or smallest elements
  • Merge K sorted lists
  • Continuous median or scheduling

Key Insight

A min-heap of size k keeps the k largest. A max-heap keeps the k smallest. In Go, negate values for max-heap.

Examples

Interval Processing

Problem Smells

  • Overlapping time ranges
  • Merge or insert intervals
  • Meeting room scheduling

Key Insight

Sort by start time, then scan left to right comparing current start with previous end.

Examples

Backtracking

Problem Smells

  • Generate all combinations or permutations
  • Constraint satisfaction (Sudoku, N-Queens)
  • Explore a decision tree

Key Insight

Choose, explore, un-choose. Prune branches early when constraints are violated.

Examples

Trie

Problem Smells

  • Prefix matching or autocomplete
  • Word dictionary with prefix queries
  • Character-by-character string building

Key Insight

Shared prefixes share tree paths. O(m) lookup where m is the word length, regardless of dictionary size.

Examples