leetcode

LeetCode patterns

Every algorithmic idea you need for a coding interview, one pattern per page: the signals that point to it, why it works, a traced example, a Python template, and the Blind 75, Grind 169, NeetCode 150 and NeetCode 250 problems it unlocks.

On this page
  1. Coverage
  2. The learning path
  3. Patterns by topic
  4. Foundations
  5. Arrays and hashing
  6. Two pointers
  7. Sliding window
  8. Stack
  9. Binary search
  10. Linked list
  11. Trees
  12. Tries
  13. Heap / priority queue
  14. Backtracking
  15. Graphs
  16. Advanced graphs
  17. 1-D dynamic programming
  18. 2-D dynamic programming
  19. Greedy
  20. Intervals
  21. Math and geometry
  22. Bit manipulation
  23. Beyond the interview

Interview problems look endless, but almost all of them are a few dozen patterns in disguise. The skill isn't memorizing solutions. It's reading a problem, noticing the signal ("sorted input and a target sum", "the best contiguous range", "every way to choose"), and reaching for the template that signal implies. Each page here teaches one pattern well enough to rebuild it from scratch, then lists the problems where it is the key idea.

Every page follows the same order: the problem shapes that signal the pattern, the intuition, the mechanics and why they are correct, a worked example traced step by step, an implementation template with variations, its complexity and tradeoffs, the problems it solves, its connections, a short check yourself quiz and references. Pages are marked intro, core or advanced. Pages still being written show as plain text.

Coverage

How many problems from each list have a finished page that teaches them.

  • Blind 75 75 / 75
  • Grind 169 169 / 169
  • NeetCode 150 150 / 150
  • NeetCode 250 250 / 250

The learning path

Seven steps from zero to interview-ready. Each step leans on the ones before it.

  1. Foundations
  2. Arrays and pointers
  3. Linear structures
  4. Trees and heaps
  5. Search
  6. Dynamic programming
  7. Interview ready

Patterns by topic

Topics follow the NeetCode roadmap, plus foundations first and a few tools that go beyond most interviews last.

Foundations

Big-O and amortized cost, the Python data structures and what each operation costs, recursion, and how to work an interview problem.

  • Complexity analysis intro

    Big-O, best and worst case, amortized cost, and reading a problem's constraints to guess the target complexity.

  • The Python toolkit intro

    list, dict, set, deque, heapq and bisect: what each operation costs and which interview idioms they enable.

  • Recursion and the call stack intro

    Base cases, the call stack, recursion trees and how to turn recursion into iteration.

  • Working an interview problem core

    Clarify, find the brute force, spot the pattern from the signals, optimize, code, test: the loop every page feeds into.

Arrays and hashing

Hash maps and sets for constant-time lookup, counting and bucketing, prefix sums, and in-place array tricks.

  • Hash maps and sets intro

    Trading memory for constant-time lookup: seen-sets, complement maps and grouping by key.

  • Counting and bucket sort core

    Frequency maps, bucketing by count and canonical keys for grouping.

  • Prefix sums and products core

    Precomputed running totals that answer range queries in constant time, and prefix sums with hash maps.

  • In-place array tricks core

    Index marking, cyclic placement and encoding strings so arrays do double duty without extra memory.

Two pointers

Two indices walking toward each other or in the same direction over a sorted or partitioned array.

  • Two pointers intro

    Converging and same-direction pointers over sorted or partitioned arrays, including 3Sum.

Sliding window

A contiguous range that grows and shrinks while an invariant about its contents holds.

Stack

Last-in, first-out matching and evaluation, and the monotonic stack for next-greater questions.

  • Stacks intro

    Matching brackets, evaluating expressions and simulating with a last-in, first-out stack.

  • Monotonic stack and deque core

    Keeping a stack sorted to answer next-greater and largest-rectangle questions, and the monotonic deque for window maxima.

Halving a sorted range, and binary search over the answer when feasibility is monotonic.

  • Binary search intro

    Halving a sorted range with a clear loop invariant, and searching rotated arrays.

  • Binary search on the answer core

    Searching the space of possible answers when a feasibility check is monotonic, and the median of two arrays.

Linked list

Pointer rewiring, dummy heads, fast and slow pointers, and lists combined with hash maps.

  • Linked list basics intro

    Reversing, merging and removing nodes with dummy heads and careful pointer rewiring.

  • Fast and slow pointers core

    Floyd's cycle detection, finding the middle, and treating an array as a linked list.

  • Linked lists with hash maps core

    The LRU cache and copying a list with random pointers: lists for order, maps for lookup.

Trees

Depth-first and breadth-first traversal, binary search trees, and building and serializing trees.

  • Tree depth-first search intro

    Preorder, inorder and postorder traversal, and returning values up the tree to answer height and path questions.

  • Tree breadth-first search intro

    Level-order traversal with a queue, and questions about levels and views of a tree.

  • Binary search trees core

    The ordering invariant, validating it, kth smallest, and lowest common ancestor in a BST.

  • Building and serializing trees advanced

    Rebuilding a tree from traversals, serializing and deserializing, and maximum path sums.

Tries

Prefix trees for word lookup, prefix search and pruning a search over a board.

  • Tries core

    Prefix trees for insert, search and prefix queries, wildcard search and pruning a board search.

Heap / priority queue

Binary heaps, top-k selection, two heaps for a running median, k-way merging and quickselect.

  • Heaps intro

    How a binary heap works, heapq idioms, and simulating schedules with a priority queue.

  • Top-k elements core

    Keeping a heap of size k to find the largest, smallest, closest or most frequent elements.

  • Two heaps advanced

    A max-heap and a min-heap balanced around the middle for a running median.

  • K-way merge core

    Merging k sorted sequences with a heap of their current heads.

  • Quickselect advanced

    Partitioning to find the kth element in expected linear time, and when it beats a heap.

Backtracking

Choose, explore, unchoose: generating subsets, permutations and combinations, and pruning constrained searches.

Graphs

Representations, DFS and BFS, grids as graphs, topological sort and union-find.

  • Graph DFS and BFS intro

    Adjacency lists, visited sets, connected components, cloning a graph and detecting cycles.

  • Grids as graphs core

    Flood fill, counting islands and multi-source BFS on a matrix.

  • Topological sort core

    Ordering a DAG with Kahn's algorithm or DFS finishing times, and detecting cycles in prerequisites.

  • Union-find core

    Disjoint sets with path compression and union by rank for connectivity and redundant edges.

Advanced graphs

Weighted shortest paths, minimum spanning trees and Eulerian paths.

  • Dijkstra's algorithm core

    Shortest paths with non-negative weights using a heap, and its variants on grids.

  • Bellman-Ford and bounded paths advanced

    Relaxing edges round by round for negative weights and paths limited to k edges.

  • Minimum spanning trees advanced

    Prim's and Kruskal's algorithms for connecting every node at minimum cost.

  • Eulerian paths advanced

    Hierholzer's algorithm for using every edge exactly once, as in reconstructing an itinerary.

1-D dynamic programming

Subproblems indexed by one position: memoization versus tabulation, knapsack and subsequence problems.

  • Dynamic programming fundamentals intro

    Overlapping subproblems, memoization versus tabulation, and the steps from brute force to DP.

  • Linear DP core

    One-dimensional recurrences: climbing stairs, house robber, decode ways and maximum product subarray.

  • Knapsack DP core

    0/1 and unbounded knapsack: coin change, partition equal subset sum and word break.

  • Longest increasing subsequence advanced

    The quadratic DP and the patience-sorting binary search that makes it n log n.

  • Palindromes core

    Expanding around centers and DP over substrings for palindromic substring questions.

2-D dynamic programming

Subproblems indexed by two positions: grids, two strings, intervals and state machines.

  • Grid DP core

    Counting and optimizing paths through a grid, and longest increasing path with memoized DFS.

  • Two-string DP advanced

    Longest common subsequence, edit distance, interleaving and distinct subsequences.

  • Interval DP advanced

    DP over ranges of an array, as in burst balloons.

  • State machine DP advanced

    Modelling holding, selling and cooldown as states: the stock problems and target sum.

Greedy

When a locally best choice is provably globally best, and Kadane's algorithm for maximum subarrays.

  • Greedy algorithms core

    Exchange arguments and when a local choice is safe: jump game, gas station and hand of straights.

  • Kadane's algorithm core

    Maximum subarray in one pass, and its circular variant.

Intervals

Sorting by start, merging and inserting intervals, and sweeping events to count overlaps.

  • Merging intervals core

    Sorting by start to merge, insert and remove overlapping intervals.

  • Sweep line advanced

    Turning intervals into start and end events to count overlaps, as in meeting rooms.

Math and geometry

Matrix rotation and spiral order, digit and modular arithmetic, fast exponentiation.

  • Matrix manipulation core

    Rotating, transposing and spiraling a matrix in place, and setting zeroes with markers.

  • Number math core

    Digit manipulation, overflow, fast exponentiation and multiplying big numbers as strings.

Bit manipulation

XOR tricks, masks, counting bits and arithmetic without arithmetic operators.

  • Bit manipulation core

    XOR to cancel pairs, masks to test and clear bits, counting bits and adding without plus.

Beyond the interview

Tools that rarely come up in interviews but solve the hard problems: segment and Fenwick trees, string matching, bitmask DP.

  • Segment trees advanced

    Range queries with point updates in logarithmic time.

  • Fenwick trees advanced

    Binary indexed trees for prefix sums under updates.

  • String matching advanced

    KMP's failure function and Rabin-Karp rolling hashes for finding a pattern in linear time.

  • Bitmask DP advanced

    DP over subsets encoded as bits, for assignment and travelling-salesman-style problems.