Algorithm patterns

LeetCode · Patterns

Algorithm patterns.

Most interview problems are a handful of ideas wearing different clothes. Pick a pattern, watch it solve a real LeetCode problem step by step, then try the practice list.

0 / 10 learned

10 patterns

FundamentalsThe everyday tools. Most easy and medium problems use one of these.

  1. 01

    Two Pointers

    Walk in from both ends and let the data tell you which side to move.

    Example

    167. Two Sum II – Input Array Is Sorted

    O(n)Medium
  2. 02

    Sliding Window

    Grow the window to the right, shrink it from the left, never look back.

    Example

    3. Longest Substring Without Repeating Characters

    O(n)Medium
  3. 03

    Binary Search

    Halve the search space every step until only the answer is left.

    Example

    704. Binary Search

    O(log n)Easy
  4. 04

    Hash Map

    Remember what you have seen so the answer is one lookup away.

    Example

    1. Two Sum

    O(n)Easy
  5. 05

    BFS / DFS

    Explore a grid or graph one neighbour at a time and mark where you have been.

    Example

    200. Number of Islands

    O(rows × cols)Medium

AdvancedBuilt on the basics, for the medium-hard problems interviews love.

  1. 06

    Dynamic Programming

    Solve each small case once, then build the big answer from the small ones.

    Example

    198. House Robber

    O(n) here; O(states × choices) in generalMedium
  2. 07

    Backtracking

    Try a choice, explore everything after it, then undo it and try the next.

    Example

    78. Subsets

    O(n · 2ⁿ) for subsetsMedium
  3. 08

    Heap / Priority Queue

    Keep only the k best so far; the weakest of them is always on top.

    Example

    215. Kth Largest Element in an Array

    O(n log k)Medium
  4. 09

    Monotonic Stack

    Keep a stack in sorted order; whatever gets popped has just found its answer.

    Example

    739. Daily Temperatures

    O(n)Medium
  5. 10

    Union-Find

    Every group has one leader; joining two groups means one leader follows the other.

    Example

    684. Redundant Connection

    ≈ O(n · α(n)), nearly linearMedium