🎯 Pattern Playbook: recognize → apply

If the problem says…Think…
Sorted array, find pair/tripletTwo pointers
Contiguous subarray/substring with a conditionSliding window (variable) / prefix sum + hashmap
Subarray sum = K (negatives allowed)Prefix sum + hashmap
”Next greater/smaller”, histogram, stock spanMonotonic stack
Sliding-window max/minMonotonic deque
Sorted / monotonic answer space, “minimize the max”Binary search (on the answer)
Top/Kth largest, merge K sorted, streaming medianHeap(s)
All combinations/permutations/subsetsBacktracking
Shortest path, unweightedBFS
Shortest path, weighted non-negativeDijkstra
Negative weightsBellman-Ford
Dependencies / orderingTopological sort
Connectivity, grouping, cycle in undirected graphUnion-Find (DSU)
Prefix/word lookup, autocompleteTrie
Overlapping intervalsSort by start + merge / sweep line / min-heap of ends
Optimal value over choices with overlapping subproblemsDP (define state → transition → base → order)
“Count ways”DP / combinatorics
Range queries with updatesSegment tree / Fenwick tree
Linked list cycle / middleFast & slow pointers
XOR tricks, subsets as masksBit manipulation
LRU/LFU, design a data structureHashMap + doubly linked list / multiple maps

DP framework

  1. State: what minimal info defines a subproblem? dp[i], dp[i][j], dp[mask]
  2. Transition: how does the state build from smaller states?
  3. Base cases
  4. Order (top-down memo first, then convert to bottom-up, then space-optimize)
  5. Answer location

Interview communication script

  1. Restate the problem and confirm the I/O, constraints, and edge cases (empty, duplicates, negatives, overflow)
  2. Walk through 1–2 examples by hand
  3. State the brute force + its complexity
  4. Optimize: name the pattern and why it fits
  5. Code cleanly (good names, helper methods)
  6. Dry-run the code on an example; test edge cases
  7. State time/space complexity; discuss follow-ups