Skip to content

Latest commit

 

History

History
354 lines (275 loc) · 17.5 KB

File metadata and controls

354 lines (275 loc) · 17.5 KB

Pattern Library - Master Algorithmic Thinking Through Internal Monologue

Reverse-engineering expertise: Learning by observing how senior engineers think through patterns


The 4-Stage Mental Pipeline

Every pattern follows the same cognitive progression:

  1. Problem → Pattern (Recognition): What triggers map problem keywords to this pattern?
  2. Pattern → Structure (What do I need?): What data structures, pointers, state variables?
  3. Structure → Behavior (How does it move?): What are the movement rules, invariants, termination conditions?
  4. Behavior → Code (Expression): Verbose form first (proof of understanding), then terse form (interview ready)

📚 Pattern Files (29 Total)

NEW: Each pattern now includes a code-to-visual mapping companion file following the CODE_MAPPING_GUIDE methodology for sticky learning.

📏 LINEAR STRUCTURES (8 patterns)

# Pattern Description Files Status
01 Sliding Window Contiguous subsequence optimization Pattern · Code Map ✅ COMPLETE
02 Two Pointers Sorted input, pairs/triplets Pattern · Code Map ✅ COMPLETE
03 Fast & Slow Pointers Cycle detection Pattern · Code Map ✅ COMPLETE
04 Merge Intervals Overlapping ranges Pattern · Code Map ✅ COMPLETE
05 Cyclic Sort [1..n] in-place sorting Pattern · Code Map ✅ COMPLETE
06 Monotonic Stack Next greater/smaller Pattern · Code Map ✅ COMPLETE
07 Prefix Sum Static range queries Pattern · Code Map ✅ COMPLETE
08 Line Sweep Event-based intervals Pattern · Code Map ✅ COMPLETE

🌳 TREES & GRAPHS (7 patterns)

# Pattern Description Files Status
09 Tree Traversals Pre/In/Post-Order DFS Pattern · Code Map ✅ COMPLETE
10 BFS Level-order, shortest path Pattern · Code Map ✅ COMPLETE
11 DFS Exhaustive search Pattern · Code Map ✅ COMPLETE
12 Topological Sort DAG ordering Pattern · Code Map ✅ COMPLETE
13 Union Find Connected components Pattern · Code Map ✅ COMPLETE
14 Trie Prefix matching Pattern · Code Map ✅ COMPLETE
15 Shortest Path Dijkstra, Bellman-Ford Pattern · Code Map ✅ COMPLETE

🎯 SELECTION & SEARCH (3 patterns)

# Pattern Description Files Status
16 Binary Search Sorted data O(log n) Pattern · Code Map ✅ COMPLETE
17 Top K / Heap Priority, streaming Pattern · Code Map ✅ COMPLETE
18 K-way Merge Sorted lists merge Pattern · Code Map ✅ COMPLETE

🔀 COMBINATORIAL & OPTIMIZATION (6 patterns)

# Pattern Description Files Status
19 Backtracking Exhaustive + pruning Pattern · Code Map ✅ COMPLETE
20 Dynamic Programming Overlapping subproblems Pattern · Code Map ✅ COMPLETE
21 Greedy Local → global optimal Pattern · Code Map ✅ COMPLETE
22 Branch & Bound Optimization + pruning Pattern · Code Map ✅ COMPLETE
23 Constraint Satisfaction Variables + domains Pattern · Code Map ✅ COMPLETE
24 Partitions Equal sum splits Pattern · Code Map ✅ COMPLETE

🔧 ADVANCED TECHNIQUES (2 patterns)

# Pattern Description Files Status
25 Bit Manipulation Bitwise operations Pattern · Code Map ✅ COMPLETE
26 String Matching KMP, Rabin-Karp Pattern · Code Map ✅ COMPLETE

📊 ADVANCED DATA STRUCTURES (3 patterns)

# Pattern Description Files Status
27 Segment Tree Range queries + updates Pattern · Code Map ✅ COMPLETE
28 Fenwick Tree Prefix sums + updates Pattern · Code Map ✅ COMPLETE
29 Suffix Array Substring matching Pattern · Code Map ✅ COMPLETE

🎓 How to Use This Library

For Learning a New Pattern

  1. Read the pattern file (01-29) to activate the mental model
  2. Study the code-map file - connect visual metaphors to actual code (sticky learning!)
  3. Focus on Internal Monologue section - how to think through problems
  4. Study Thought Narratives - solving problems as if first time
  5. Practice LeetCode problems from the pattern file (organized by difficulty)

For Deep Understanding (Code-to-Visual Mapping)

  1. Start with the visual - Read pattern file's mermaid diagrams
  2. Read the code - Study implementation examples in pattern file
  3. Use the code-map - See exact line-by-line visual-to-code correspondence
  4. Test yourself - Can you explain code using visual metaphor?
  5. Trace an example - Step through execution with both visual and code side-by-side

For Interview Prep

  1. Review Self-Check questions at top of each pattern
  2. Study Interview Communication Template - what to say in interviews
  3. Practice Decision Trees - 5-second pattern recognition
  4. Solve Medium → Hard problems from LeetCode tables

For Review

  1. Check Self-Check questions only - if all checked, skip to next pattern
  2. If any unchecked - re-read relevant sections
  3. Test yourself: Can you explain structure + behavior in plain language?

When Stuck on a Problem

  1. Read pattern keywords in problem description
  2. Use Decision Trees to identify the right pattern
  3. Open that pattern file and read Thought Narratives
  4. Apply the 4-Stage Pipeline to think through the solution

🧠 Philosophy

"Code is just the expression of structure and behavior. If I can't articulate the structure (what exists) and behavior (how it moves) in plain language, I don't understand the pattern—I'm just copying syntax."

The goal: Internalize the thinking process, not just code templates.

What makes this different:

  • ❌ Not just code templates
  • ❌ Not just problem lists
  • Thought narratives - how senior engineers think
  • Internal monologue - the self-talk during problem-solving
  • Pattern recognition - 5-second trigger mapping
  • Interview communication - what to say out loud

🗺️ Pattern Concept Tree

A visual mental model of how patterns relate:

graph TB
    Root["Algorithmic Patterns<br/>(29 patterns)"]

    subgraph Linear["📏 LINEAR STRUCTURES (8)"]
        SW["01. Sliding Window"]
        TP["02. Two Pointers"]
        FSP["03. Fast & Slow"]
        MI["04. Merge Intervals"]
        CS["05. Cyclic Sort"]
        MS["06. Monotonic Stack"]
        PS["07. Prefix Sum"]
        LS["08. Line Sweep"]
    end

    subgraph TreesGraphs["🌳 TREES & GRAPHS (7)"]
        TT["09. Tree Traversals"]
        BFS["10. BFS"]
        DFS["11. DFS"]
        Topo["12. Topological Sort"]
        UF["13. Union Find"]
        Trie["14. Trie"]
        SP["15. Shortest Path"]
    end

    subgraph Selection["🎯 SELECTION & SEARCH (3)"]
        BS["16. Binary Search"]
        Heap["17. Top K / Heap"]
        KM["18. K-way Merge"]
    end

    subgraph Combo["🔀 COMBINATORIAL (6)"]
        Back["19. Backtracking"]
        DP["20. Dynamic Programming"]
        Greedy["21. Greedy"]
        BB["22. Branch & Bound"]
        CSP["23. Constraint Satisfaction"]
        Part["24. Partitions"]
    end

    subgraph Advanced["🔧 ADVANCED (5)"]
        Bit["25. Bit Manipulation"]
        SM["26. String Matching"]
        SegTree["27. Segment Tree"]
        Fenwick["28. Fenwick Tree"]
        Suffix["29. Suffix Array"]
    end

    Root --> Linear
    Root --> TreesGraphs
    Root --> Selection
    Root --> Combo
    Root --> Advanced

    style Root fill:#4a1942,stroke:#f472b6,stroke-width:3px,color:#e0e0e0
    style SW fill:#1e3a5f,stroke:#22d3ee,stroke-width:2px,color:#e0e0e0
    style TP fill:#1e3a5f,stroke:#22d3ee,stroke-width:2px,color:#e0e0e0
    style SegTree fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
    style Fenwick fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
    style Suffix fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
Loading

🔍 Decision Tree: Which Pattern to Use?

Use this for 5-second pattern recognition:

flowchart TD
    Start{Problem Type?}

    Start -->|Array/String| Array
    Start -->|Tree/Graph| Graph
    Start -->|Optimization| Opt
    Start -->|Range Queries| Range

    Array{Array Pattern?}
    Array -->|Contiguous subarray| SW["01. Sliding Window"]
    Array -->|Two elements, sorted| TP["02. Two Pointers"]
    Array -->|Overlapping intervals| MI["04. Merge Intervals"]
    Array -->|Numbers [1..n]| CS["05. Cyclic Sort"]
    Array -->|Next greater/smaller| MS["06. Monotonic Stack"]
    Array -->|Cumulative sums| PS["07. Prefix Sum"]

    Graph{Graph Pattern?}
    Graph -->|Level-by-level| BFS["10. BFS"]
    Graph -->|All paths| DFS["11. DFS"]
    Graph -->|DAG dependencies| Topo["12. Topological Sort"]
    Graph -->|Connected components| UF["13. Union Find"]
    Graph -->|Shortest path| SP["15. Dijkstra/Bellman-Ford"]
    Graph -->|Prefix strings| Trie["14. Trie"]

    Opt{Optimization Type?}
    Opt -->|Generate all| Back["19. Backtracking"]
    Opt -->|Overlapping subproblems| DP["20. Dynamic Programming"]
    Opt -->|Local → Global optimal| Greedy["21. Greedy"]
    Opt -->|Discrete optimization| BB["22. Branch & Bound"]
    Opt -->|Variables + Constraints| CSP["23. Constraint Satisfaction"]
    Opt -->|Equal sum groups| Part["24. Partitions"]

    Range{Update Pattern?}
    Range -->|Static array| PS["07. Prefix Sum"]
    Range -->|Point updates + range sum| Fenwick["28. Fenwick Tree"]
    Range -->|Range updates + any op| SegTree["27. Segment Tree"]
    Range -->|Pattern matching| Suffix["29. Suffix Array"]

    style Start fill:#4a1942,stroke:#f472b6,stroke-width:3px,color:#e0e0e0
    style SW fill:#1a4a3a,stroke:#4ade80,stroke-width:2px,color:#e0e0e0
    style SegTree fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
    style Fenwick fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
    style Suffix fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
Loading

🔗 Pattern Relationships

How patterns build on each other:

graph LR
    subgraph Foundation["Foundation Patterns"]
        TP["02. Two Pointers"]
        BS["16. Binary Search"]
        DFS["11. DFS"]
    end

    subgraph Derived["Derived Patterns"]
        SW["01. Sliding Window<br/>(extends Two Pointers)"]
        Back["19. Backtracking<br/>(extends DFS)"]
        DP["20. DP<br/>(memoized recursion)"]
    end

    subgraph RangeQueries["Range Query Evolution"]
        PS["07. Prefix Sum<br/>(static)"]
        Fenwick["28. Fenwick Tree<br/>(prefix sums + updates)"]
        SegTree["27. Segment Tree<br/>(any operation + updates)"]
    end

    subgraph StringOps["String Pattern Matching"]
        Naive["Naive O(nm)"]
        KMP["26. KMP O(n+m)"]
        Suffix["29. Suffix Array<br/>(multiple patterns)"]
    end

    TP --> SW
    DFS --> Back
    Back --> DP

    PS --> Fenwick
    Fenwick -.->|More general| SegTree
    PS -.->|No updates| SegTree

    Naive --> KMP
    KMP -.->|Many queries| Suffix

    style PS fill:#4a3a0a,stroke:#fbbf24,stroke-width:2px,color:#e0e0e0
    style Fenwick fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
    style SegTree fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
    style Suffix fill:#1a4a3a,stroke:#4ade80,stroke-width:3px,color:#e0e0e0
Loading

📊 Decision Matrices

Range Query Structures: When to Use What

Feature Prefix Sum Fenwick Tree Segment Tree
Point Update ❌ O(n) ✅ O(log n) ✅ O(log n)
Range Update ✅ O(log n) w/ lazy
Range Sum Query ✅ O(1) ✅ O(log n) ✅ O(log n)
Range Min/Max Query ✅ O(log n)
Arbitrary Associative Op ✅ O(log n)
Space Complexity O(n) O(n) O(4n)
Code Complexity Simple Medium Complex
Use When Read-only Prefix sums + updates Any range op + updates
Pattern # 07 28 27

String Pattern Structures: When to Use What

Feature Trie Suffix Array KMP
Multiple Patterns ✅ Prefixes ✅ Substrings ❌ Single
Build Time O(total chars) O(n log n) O(m)
Query Time O(m) O(m log n) O(n)
Space O(alphabet × nodes) O(n) O(m)
Use When Autocomplete, prefix match Many substring queries One-time search
Pattern # 14 29 26

🚀 Quick Reference

Pattern by Number: 01 · 02 · 03 · 04 · 05 · 06 · 07 · 08 · 09 · 10 · 11 · 12 · 13 · 14 · 15 · 16 · 17 · 18 · 19 · 20 · 21 · 22 · 23 · 24 · 25 · 26 · 27 · 28 · 29

By Category:


🧭 Navigation

↑ Back: Library 2025

→ Related:

⚡ Start Learning: 01. Sliding Window (Pattern · Code Map)