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 as a target shape: TL;DR -> Core Basics -> recognition cues -> Diagram (Mermaid) -> optional step-by-step visual enrichment -> Understanding -> generic recipe -> complexity -> Memory Hooks -> Study Pattern (SDE3+) -> Go + JS template -> variants -> representative problems -> anti-patterns.


How to read a pattern page for clarity

  1. Read TL;DR and Core Basics — one minute to know what object you are maintaining (indices, queue, heap, trie node, …).
  2. Skim Recognition Cues — tie phrases from problem statements to this pattern.
  3. Study the Diagram — it is the fastest way to rebuild the algorithm from memory.
  4. Walk the Generic Recipe out loud with a tiny example before looking at code.
  5. Use Anti-patterns to see the common wrong turns (off-by-one, wrong heap order, missing undo in backtracking).

Code at the bottom is for fluency; Understanding + Diagram are what make the concept stick.


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 or sorted after preprocessing? -> Two Pointers, Binary Search, Merge Intervals, or Greedy by sorted key.
  • Contiguous substring/subarray with a maintained condition? -> Sliding Window.
  • Linked list with "cycle/middle/k-from-end"? -> Fast & Slow; with "reverse/sublist/k-group"? -> Linked List Reversal.
  • Values are supposed to live in [1..n] or index positions? -> Cyclic Sort.
  • "kth ...", "top k", streaming rank, or repeated next-best candidate? -> Top-K / Heap, Monotonic Queue, or Divide & Conquer for partition-based selection.
  • Tree / graph traversal? -> BFS for shortest/levels; DFS for all paths, components, post-order, or backtracking.
  • Dependency / ordering / prerequisites? -> Topological Sort.
  • Dynamic connectivity / groups / redundant edge? -> Union-Find.
  • Prefix queries on strings or dictionary pruning? -> Trie.
  • "Next greater / smaller", span, histogram, or window max? -> Monotonic Stack / Queue.
  • Range sum, repeated subarray sum, or range update? -> Prefix Sum / Difference Array.
  • XOR, bit count, bitmask subsets, or arithmetic without operators? -> Bit Manipulation.
  • "Optimize/count ways with overlapping subproblems"? -> Dynamic Programming.
  • "Make local choice" with a provable exchange/frontier argument? -> Greedy.
  • Split into independent halves/ranges and combine results? -> Divide & Conquer.

Mark this page when you finish learning it.

Last updated on

Spotted something unclear or wrong on this page?

On this page