Skip to content

Algorithm Patterns

Instead of memorizing individual solutions, learning these patterns helps you recognize problem types, apply the right technique, and build lasting problem-solving intuition.

  • Sliding Window


    Optimize contiguous sequences. Transform O(n^2) to O(n) by maintaining a moving window.

    Learn Pattern

  • Two Pointers


    Traverse from both ends or move pointers together. Essential for sorted arrays and palindromes.

    Learn Pattern

  • Fast & Slow Pointers


    Detect cycles and find middle elements using different pointer speeds.

    Learn Pattern

  • Dynamic Programming


    Break complex problems into overlapping subproblems. Cache results to avoid redundant work.

    Learn Pattern

  • Backtracking


    Explore all possibilities by building candidates incrementally. Prune invalid paths early.

    Learn Pattern

  • Binary Search on Answer


    When the answer is monotonic, binary search the solution space instead of enumerating.

    Learn Pattern

  • Greedy


    Make locally optimal choices that lead to globally optimal solutions.

    Learn Pattern

  • Heap / Priority Queue


    Efficiently surface minimum or maximum elements for scheduling and top-k problems.

    Learn Pattern

  • Monotonic Stack


    Find next greater/smaller elements in O(n) using stack invariants.

    Learn Pattern

  • Graph Traversal


    BFS for shortest paths, DFS for exhaustive exploration. Master both.

    Learn Pattern

  • Trie


    Tree structure for efficient prefix operations. Essential for autocomplete and dictionaries.

    Learn Pattern

  • Space Complexity


    Analyze and optimize memory usage. In-place algorithms and space-time tradeoffs.

    Learn Concepts


Pattern Selection Guide

┌──────────────────────────────────────────────────────────────────────────────┐
│                         WHICH PATTERN SHOULD I USE?                          │
└──────────────────────────────────────────────────────────────────────────────┘

  "Find contiguous subarray/substring..."
    └──▶ SLIDING WINDOW

  "Find pair/triplet in sorted array..."
    └──▶ TWO POINTERS

  "Detect cycle or find middle..."
    └──▶ FAST & SLOW POINTERS

  "How many ways..." or "Minimum/Maximum..."
    └──▶ DYNAMIC PROGRAMMING

  "Generate all combinations/permutations..."
    └──▶ BACKTRACKING

  "Find minimum/maximum that satisfies..."
    └──▶ BINARY SEARCH ON ANSWER

  "Find k largest/smallest..."
    └──▶ HEAP / PRIORITY QUEUE

  "Next greater/smaller element..."
    └──▶ MONOTONIC STACK

  "Shortest path" or "Connected components..."
    └──▶ GRAPH TRAVERSAL (BFS/DFS)

  "Prefix matching" or "Autocomplete..."
    └──▶ TRIE

Pattern Reference Table

Pattern Time Space Key Indicator Practice
Sliding Window O(n) O(1) Contiguous sequences LC 3
Two Pointers O(n) O(1) Sorted input, pairs LC 167
Fast & Slow O(n) O(1) Cycles, linked lists LC 141
Dynamic Programming Varies O(n) Overlapping subproblems LC 322
Backtracking O(2^n) O(n) All combinations LC 78
Binary Search on Answer O(n log k) O(1) Monotonic feasibility LC 875
Greedy O(n log n) O(1) Local = global optimal LC 45
Heap O(n log k) O(k) Top-k, scheduling LC 347
Monotonic Stack O(n) O(n) Next greater/smaller LC 739
Graph Traversal O(V+E) O(V) Connectivity, paths LC 200
Trie O(m) O(n*m) Prefix operations LC 208

Each Pattern Section Includes

Conceptual Overview

When and why to use the pattern, with visual diagrams showing the core mechanism.

Step-by-Step Approach

Systematic methodology you can apply to any problem that fits the pattern.

LeetCode Examples

Real problems with complete solutions, complexity analysis, and detailed walkthroughs.

Common Pitfalls

Mistakes to avoid and edge cases to consider.


Suggested Learning Order

  1. Week 1-2: Sliding Window, Two Pointers, Hash Tables
  2. Week 3-4: Dynamic Programming (start with 1D problems)
  3. Week 5-6: Backtracking, BFS/DFS
  4. Week 7-8: Binary Search variants, Heaps
  5. Week 9+: Monotonic Stack, Tries, Advanced DP