Knowledge Guide
HomeDSA

🔎 Pattern Recognition — DSA

Read a problem, spot the signal, pick the technique — your triage map for DSA.

How to use this guide: read the problem statement, scan for the signals below (they are literal words and shapes that appear in prompts), and jump to the technique they point at. Most problems announce their pattern in one or two phrases. Match the loudest signal first, then confirm with the constraints (sortedness, "in place", "contiguous", input size).

SIGNAL → TECHNIQUE MAPPINGS

1. Words about contiguity and ranges

2. Words about pairs, order, and sortedness

3. Words about search and monotonic answer spaces

4. Words about lookup, counting, and grouping

5. Words about prefixes and strings of strings

6. Words about LIFO / FIFO ordering

7. Words about "top K" and K sorted sources

8. Words about hierarchy and relationships

9. Words about a 2D grid you transform

10. Words about relinking nodes

11. Words about enumeration and constrained choices

12. Words about splitting and self-similarity

13. Words about cost and growth (meta-signal)

TRIAGE ORDER (try in this sequence)

  1. Read the literal trigger words first. "contiguous" → Sliding Window; "all / every way" → Backtracking; "top K / K-th" → Heap; "starts-with / prefix" → Trie; "most recent / nested pairs" → Stack; "arrival order / level by level" → Queue/BFS. These near-uniquely name the pattern.
  2. Check sortedness. Sorted (or sortable) + pair/triplet/target → Two Pointers. Sorted + find/exist → Binary Search. "Minimize the max / maximize the min" → Binary Search on the answer.
  3. Check for O(1) lookup or counting needs. Membership/dedupe → HashSet; frequency/anagram/"make equal" → HashMap or int[26]; interval overlap counting → difference array.
  4. Identify the structure. grid → Matrix or grid-BFS; relationships/reachability/prerequisites → Graph; hierarchy/one root → Tree; relink-in-place nodes → Linked List.
  5. If it is "best/count over a sequence of decisions": try Greedy first (cheaper); if a locally optimal choice can be wrong, fall back to Dynamic Programming (confirm optimal substructure + overlapping subproblems).
  6. If you must enumerate every solution: Backtracking (with pruning).
  7. If the problem decomposes into sub-ranges/halves: Divide & Conquer / Recursion; write the recurrence.
  8. Always sanity-check the complexity against the input size (Foundations): if n is up to ~10⁵–10⁶ you need O(n) or O(n log n), which often rules a pattern in or out before you write any code.