THN Interview Prep

Patterns — Recognition Layer

The 20 patterns that recur across ~90% of MAANG/MNC SDE3+ DSA interviews. Master these, and 80% of new problems become "oh, this is just X with a twist".

Each pattern file follows the pattern template: TL;DR → recognition cues → Diagram (Mermaid) → mental model → generic recipe → Go + JS template → variants → representative problems → anti-patterns.


The 20 patterns

#PatternWhen you see itGuide
01Two Pointerssorted array, pair/triplet sums, in-place dedupeTwo Pointers
02Sliding Windowlongest/shortest substring with property, fixed-size subarraySliding Window
03Fast & Slow Pointerslinked list cycle, find middle, duplicate numberFast & Slow Pointers
04Binary Searchsorted array, "find smallest X such that ..." (search on answer)Binary Search
05BFSshortest path in unweighted graph, level-order treeBFS
06DFS / Backtrackingcombinations/permutations, all paths, "is there a way"DFS / Backtracking
07Top-K / Heap"k largest/smallest/most frequent"Top-K / Heap
08Merge Intervalsintervals input, overlap/mergeMerge Intervals
09Cyclic Sortnumbers in [1..N], find missing/duplicate in O(n) O(1)Cyclic Sort
10In-place Linked List Reversalreverse list / sublist / k-groupIn-place Linked List Reversal
11Tree DFS / DP-on-Treespath sums, diameter, LCA, serializeTree DFS / DP-on-Trees
12Topological Sortordering with prerequisites, DAGTopological Sort
13Union-Find (DSU)dynamic connectivity, count components, redundant edgesUnion-Find (DSU)
14Trieprefix queries, word search, autocompleteTrie
15Monotonic Stack / Queue"next greater / smaller", sliding-window maxMonotonic Stack / Queue
16Prefix Sum / Difference Arrayrange sums, range updatesPrefix Sum / Difference Array
17Bit ManipulationXOR tricks, subsets, single numberBit Manipulation
18Dynamic Programmingoptimal substructure + overlapping subproblemsDynamic Programming
19Greedylocal choice -> global optimum, exchange argumentGreedy
20Divide & Conquerrecurse halves + merge (sort, search)Divide & Conquer

Decision tree (cheat)

See Pattern recognition cheatsheet for a flowchart.

Quick triage:

  • Sorted input? -> Two Pointers / Binary Search
  • Substring/subarray with constraint? -> Sliding Window
  • Linked list with "cycle/middle/k-from-end"? -> Fast & Slow
  • "kth ..." or "top k"? -> Heap (or Quickselect)
  • Tree / graph traversal? -> BFS (shortest, levels) vs DFS (all paths, post-order)
  • Dependency / ordering? -> Topo Sort
  • Dynamic connectivity / groups? -> Union-Find
  • Prefix queries on strings? -> Trie
  • "Next greater / smaller"? -> Monotonic Stack
  • Range sum / update? -> Prefix Sum / Difference Array
  • "Optimize X with overlapping subproblems"? -> DP
  • "Make local greedy choice"? -> Greedy

Last updated on

Spotted something unclear or wrong on this page?

On this page