Skip to content
Back to Book
Free Chapter

Introduction — Why Patterns Beat Memorization

Prerequisites: None (start here)

There are over 2,500 problems on LeetCode. If you try to memorize solutions to each one, you'll need years — and you'll forget them almost as fast as you learn them.

But here's the secret that top competitive programmers and FAANG interviewers know: there are only a handful of core patterns. Every problem is a variation of one (or a combination) of these patterns.

Consider this analogy: learning individual problem solutions is like memorizing every sentence in a language. Learning patterns is like learning grammar rules — once you know the rules, you can construct any sentence.

The Three Levels of Problem-Solving

Level 1: Memorizer — "I've seen this exact problem before, I know the solution."

  • Fragile. Change one constraint and they're stuck.
  • Requires seeing thousands of problems.
  • High effort, low transfer.

Level 2: Pattern Matcher — "This problem has the same structure as problems I've solved before."

  • Robust. Handles variations naturally.
  • Requires learning each pattern deeply.
  • Moderate effort, high transfer.

Level 3: Problem Decomposer — "I can break this into subproblems I know how to solve."

  • Expert level. Combines patterns fluently.
  • Requires solid pattern foundation + practice.
  • This book gets you to Level 2, with the foundation for Level 3.

How This Book Is Different: The 7-Layer Skill Stack

Most algorithm books explain how to solve problems. This book teaches you how to recognize which approach to use — which is the actual skill tested in interviews.

Every chapter follows the same 7-layer structure designed to build pattern recognition, not memorization:

  1. The Signal — The exact keywords and constraints that fingerprint each pattern in problem statements. See these clues, know the pattern instantly.
  2. When NOT to Use — 6 boundary rules per pattern that teach you where each technique breaks down. No other book has structured per-pattern boundary guidance.
  3. The Mental Model — A beginner-vs-expert table that makes the cognitive shift visible. See what experts notice that beginners miss.
  4. Templates with WHY — Every line of template code annotated with reasoning, not just "here's the code."
  5. Trap Callouts — Named trap callouts on every practice problem — the specific bug most people write on their first attempt. Each has an explanation and a one-line fix.
  6. Interview Scripts — Verbatim phrases for every problem to say out loud to your interviewer. Communication matters as much as code.
  7. Practice Problems — Up to five difficulty tiers per pattern: Easy (direct application), Medium (with a twist), Hard (pushing the limits), Variant (a different angle on the same idea), and Pattern Combination (blending two patterns together).

Each chapter also includes a Visual Walkthrough (step-by-step execution trace), a Complexity Table, a Common Mistakes section, and a Chapter Summary with links to related patterns.

A note on chapter numbering: Chapter numbers reflect the filename and study priority, not sequential order. Parts group chapters by data structure family. Use the Table of Contents or Decision Flowchart to navigate.


The Master Decision Tree

This is the most important page in the book. Bookmark it, photograph it, tattoo it on your arm (not medical advice).

When you read a problem statement, follow this tree:

START: Read the problem statement
└─ What is the PRIMARY input?

   ARRAY / STRING
   └─ Sorted?
      ├─ Yes → What do you need?
      │  ├─ Pair/triplet with target ──────► TWO POINTERS
      │  └─ Find element/boundary ─────────► BINARY SEARCH
      └─ Not sorted → What result?
         ├─ Pair/complement/target ────────► TWO SUM PATTERN (Ch 6)
         ├─ Frequency/grouping/duplicates ─► HASH MAP COUNTING (Ch 5)
         ├─ Max/min subarray ──────────────► KADANE'S
         ├─ Subarray with constraint ──────► SLIDING WINDOW
         ├─ Top K elements ────────────────► HEAP / BUCKET SORT
         ├─ Range sum queries ─────────────► PREFIX SUM
         ├─ Next greater/smaller ──────────► MONOTONIC STACK
         ├─ Numbers in [0,n] ──────────────► CYCLIC SORT
         ├─ Locally optimal ───────────────► GREEDY
         └─ All subsets/perms ─────────────► BACKTRACKING

   LINKED LIST
   └─ What operation?
      ├─ Cycle / find middle ──────────────► FAST & SLOW POINTERS
      ├─ Reverse all or part ──────────────► REVERSAL PATTERN
      └─ Merge K sorted ──────────────────► K-WAY MERGE

   TREE
   └─ What traversal?
      ├─ Level by level ───────────────────► BFS
      ├─ Path / depth / validate ──────────► DFS
      ├─ Optimize max path sum ────────────► DP ON TREES
      └─ Prefix matching / dictionary? ────► TRIE (Ch 23)

   GRAPH
   └─ What do you need?
      ├─ Shortest path ────────────────────► BFS
      ├─ Connected components ─────────────► DFS or UNION-FIND
      ├─ Dependencies / ordering ──────────► TOPOLOGICAL SORT
      └─ All possibilities ────────────────► BACKTRACKING

   INTERVALS / RANGES ─────────────────────► MERGE INTERVALS

   NUMBER / MATH
   └─ What kind?
      ├─ Bit operations ───────────────────► BIT MANIPULATION
      ├─ Running median ───────────────────► TWO HEAPS
      └─ Count ways / optimize ────────────► DYNAMIC PROGRAMMING

   DP TYPE?
   └─ What recurrence?
      ├─ Linear (stairs, robber) ──────────► FIBONACCI DP
      ├─ Take/skip + capacity ─────────────► 0/1 or UNBOUNDED KNAPSACK
      ├─ Two sequences ────────────────────► LCS PATTERN
      └─ 2D grid path/cost ────────────────► GRID DP

How to Use This Tree

  1. Read the problem statement carefully. Underline the input type and what you're asked to find.
  2. Start at the top of the tree. Follow the branch that matches your input.
  3. Answer each question honestly. If you're unsure, try both branches.
  4. When you reach a pattern name, flip to that chapter for the full walkthrough.
  5. If the pattern doesn't work, go back up and try a different branch. Some problems combine two patterns.

Practice Makes Permanent

The decision tree becomes second nature after about 50 problems. Here's a practice routine:

  • Week 1-2: Do 2-3 problems per pattern for the first 7 patterns (Array & String, Hashing)
  • Week 3-4: Move to Sorting, Linked List, and Tree patterns
  • Week 5-6: Tackle Graph and DP patterns
  • Week 7-8: Mixed practice — random problems, identify the pattern first

For each problem, BEFORE writing any code:

  1. Identify the input type
  2. Walk the decision tree
  3. Name the pattern you think applies
  4. Only THEN start coding

This "name it before you code it" discipline is what separates pattern matchers from memorizers.

Note on difficulty labels: Practice problem difficulty levels (Easy/Medium/Hard) reflect our assessment of conceptual difficulty within each pattern's context and may differ from LeetCode's official classifications.


Pattern Priority Guide: What to Study First

Study Priority Tiers

Tier 1 — Must Know (Study these first, ~30 hours total) These patterns appear in 60%+ of coding interviews combined.

Pattern Interview Freq Time to Master Chapter
Hash Map Counting ⬛⬛⬛⬛⬛ (5/5) ~2 hours Ch 5
Two Pointers ⬛⬛⬛⬛⬛ (5/5) ~3 hours Ch 1
Sliding Window ⬛⬛⬛⬛⬛ (5/5) ~3 hours Ch 2
DFS ⬛⬛⬛⬛⬛ (5/5) ~4 hours Ch 13
BFS ⬛⬛⬛⬛⬛ (5/5) ~3 hours Ch 12
Binary Search ⬛⬛⬛⬛⬛ (5/5) ~4 hours Ch 7
Greedy ⬛⬛⬛⬛⬜ (4/5) ~3 hours Ch 28
Two Sum Pattern ⬛⬛⬛⬛⬜ (4/5) ~2 hours Ch 6
Backtracking ⬛⬛⬛⬛⬜ (4/5) ~4 hours Ch 14
Fibonacci DP ⬛⬛⬛⬛⬜ (4/5) ~3 hours Ch 19

Tier 2 — Should Know (~20 hours total) High-value patterns that appear in 5-10% of interviews each.

Pattern Interview Freq Time to Master Chapter
Prefix Sum ⬛⬛⬛⬛⬜ (4/5) ~2 hours Ch 3
Merge Intervals ⬛⬛⬛⬛⬜ (4/5) ~2 hours Ch 8
Topological Sort ⬛⬛⬛⬛⬜ (4/5) ~3 hours Ch 15
0/1 Knapsack ⬛⬛⬛⬛⬜ (4/5) ~4 hours Ch 17
Kadane's ⬛⬛⬛⬜⬜ (3/5) ~1 hour Ch 4
Bucket Sort / Top-K ⬛⬛⬛⬜⬜ (3/5) ~2 hours Ch 9
Monotonic Stack ⬛⬛⬛⬜⬜ (3/5) ~3 hours Ch 22
Trie ⬛⬛⬛⬜⬜ (3/5) ~3 hours Ch 23

Tier 3 — Nice to Know (~22 hours total) Patterns for specific problem types; study after mastering Tiers 1-2.

Pattern Interview Freq Time to Master Chapter
Fast & Slow Pointers ⬛⬛⬛⬜⬜ (3/5) ~2 hours Ch 10
Linked List Reversal ⬛⬛⬛⬜⬜ (3/5) ~1 hour Ch 11
Union-Find ⬛⬛⬛⬜⬜ (3/5) ~3 hours Ch 16
LCS ⬛⬛⬛⬜⬜ (3/5) ~2 hours Ch 20
Two Heaps ⬛⬛⬜⬜⬜ (2/5) ~2 hours Ch 26
K-way Merge ⬛⬛⬜⬜⬜ (2/5) ~2 hours Ch 27
Unbounded Knapsack ⬛⬛⬜⬜⬜ (2/5) ~2 hours Ch 18
Bit Manipulation ⬛⬛⬜⬜⬜ (2/5) ~2 hours Ch 24
Cyclic Sort ⬛⬛⬜⬜⬜ (2/5) ~1 hour Ch 25
DP on Trees ⬛⬛⬜⬜⬜ (2/5) ~3 hours Ch 21
Grid DP ⬛⬛⬜⬜⬜ (2/5) ~2 hours Ch 29

Pattern Dependency Graph

Learn patterns in this order. Lines (───) connect related patterns; study the left pattern before the right one. Vertical lines () show deeper prerequisites.

Hash Map Counting ─── Two Sum Pattern

Two Pointers ─── Sliding Window

Prefix Sum ─── Kadane's

Binary Search        Merge Intervals ─── Greedy

       Bucket Sort / Top-K ─── Two Heaps
                │
             K-way Merge

Fast-Slow Pointers ─── Linked List Reversal

         BFS ─── DFS ─── Backtracking
          │       │
  Topological   Union-Find
    Sort          │
                DFS ─── DP on Trees

Fibonacci DP ─── 0/1 Knapsack ─── Unbounded Knapsack
     │
    LCS ─── Grid DP

Monotonic Stack    Trie    Bit Manipulation    Cyclic Sort
(independent — study anytime)

Suggested Study Plans

1-Week Emergency (for interviews this week): 10 highest-ROI patterns, ~12 hours — see Study Calendar 2-Week Sprint (for imminent interviews): Tier 1 only — 10 patterns, ~30 hours 4-Week Standard: Tiers 1+2 — 18 patterns, ~50 hours 8-Week Comprehensive: All patterns + combinations — ~73 hours


How to Use This Book

For Interview Prep (4-8 weeks)

Follow chapters in the order listed in your study plan. Each pattern builds on concepts from earlier patterns. Do all practice problems for each pattern. Visit algoark.io for animated walkthroughs of each pattern.

As a Reference

Use the Quick Reference Card in the Appendix to quickly look up a pattern. Use the Master Decision Tree when you encounter a new problem. Each chapter is self-contained — jump to any pattern you need.

With the Companion Tool — AlgoArk

Visit algoark.io for animated walkthroughs of each pattern. The animations show the algorithm executing step by step with play/pause/step controls. Use this to build intuition for how each pattern moves through data.


Let's begin with the most common and versatile patterns: those for arrays and strings.


Chapter Summary

  • Pattern-based approach beats memorization: These core patterns cover most interview problems.
  • Three levels of problem-solving: Memorizer (fragile), Pattern Matcher (robust), Problem Decomposer (expert).
  • Use the Master Decision Tree: identify input type, follow branches, name the pattern before coding.
  • Practice "name it before you code it" — identify the pattern before writing any solution.
  • Study plan: 2–3 problems per pattern for first 7 patterns, then expand to trees, graphs, and DP.

Enjoying this preview?

Every DSA pattern, practice problems at every level, trap callouts, and study plans.

Get the Book — $9.99