Skip to content
BytePatterns
All lessons

Interview patterns cheat sheet

The coding-interview patterns worth knowing: when to reach for each, the signals in a problem statement that give it away, and a lesson and practice for each.

Updated 23 patternsa lesson and practice for eachPrints cleanly — use your browser's print

Read the problem statement first, then scan the signals: the phrases below are the ones that give a pattern away. The cost line is what the pattern typically achieves — the number to say out loud before you start writing code.

Arrays and strings

  • Two pointers

    A sorted array or a string where a pair, a partition or a palindrome is decided from both ends.

    Signals

    • sorted array + find a pair / triple with a target sum
    • remove duplicates or move items in place
    • palindrome, reverse, or compare from both ends

    Typical cost: O(n) time, O(1) space

    Lesson
    Two Pointers · Arrays
  • Sliding window

    The answer is a contiguous run and growing or shrinking it by one element updates the state cheaply.

    Signals

    • longest / shortest substring or subarray that…
    • at most k distinct, without repeats, sum ≥ target
    • every window of size k

    Typical cost: O(n) time, O(k) space

    Lesson
    Sliding Window · Arrays
  • Prefix sums

    Many range-sum questions over the same array, or a subarray sum that must equal k.

    Signals

    • sum of elements between i and j, asked repeatedly
    • count subarrays with sum = k (negatives allowed)
    • product of everything except self

    Typical cost: O(n) build, O(1) per query

    Lesson
    Prefix Sums · Arrays
  • Hash map lookups

    You keep asking "have I seen this before?" or "how many of these are there?" inside a loop.

    Signals

    • two items that add up / pair up (unsorted input)
    • anagrams, frequencies, first unique, duplicates
    • group items by a canonical key

    Typical cost: O(n) time, O(n) space

    Lesson
    Two Sum · Hash Tables

Linked lists and stacks

  • Fast & slow pointers

    A linked list (or a function that maps a value to the next) where you need a cycle, a middle or the k-th from the end.

    Signals

    • detect a cycle / where the cycle starts
    • middle of the list in one pass
    • remove the n-th node from the end

    Typical cost: O(n) time, O(1) space

    Lesson
    Fast and Slow Pointers · Linked Lists
  • Monotonic stack

    For each element you need the next (or previous) element that is greater or smaller.

    Signals

    • next greater / next warmer / next smaller
    • span, visible buildings, days until…
    • largest rectangle in a histogram

    Typical cost: O(n) time, O(n) space

    Lesson
    Monotonic Stack · Stacks & Queues

Heaps

  • Top-k elements

    You need the k largest, smallest, closest or most frequent — not a full sort.

    Signals

    • k largest / k smallest / k-th largest
    • k closest points, k most frequent
    • a stream where k stays small

    Typical cost: O(n log k) time, O(k) space

    Lesson
    Top K Elements · Heaps
  • Two heaps

    You need the middle of a changing collection: a max-heap for the lower half, a min-heap for the upper.

    Signals

    • running median of a stream
    • median of every window
    • pick the best affordable item as the budget grows

    Typical cost: O(log n) per insert, O(1) per median

    Lesson
    Two Heaps: Running Median · Two Heaps & K-Way Merge
  • K-way merge

    Several sorted lists and you want them in one order, or the k-th item across them.

    Signals

    • merge k sorted lists / arrays
    • k-th smallest in a sorted matrix
    • smallest range covering one item from each list

    Typical cost: O(N log k) time, O(k) space

    Lesson
    K-Way Merge · Two Heaps & K-Way Merge

Intervals and greedy

  • Merge intervals

    Ranges that may overlap: sort by start, then sweep once comparing each range with the last one kept.

    Signals

    • meetings, bookings, time ranges, spans
    • merge / insert / count overlapping intervals
    • minimum rooms, maximum concurrent

    Typical cost: O(n log n) time (the sort), O(n) space

    Lesson
    Merge Intervals · Intervals
  • Greedy choice

    Taking the locally best option never has to be undone — and you can argue why (an exchange argument).

    Signals

    • maximum number of non-overlapping events
    • can you reach the end / fewest jumps
    • a circular route where a running total must stay ≥ 0

    Typical cost: Usually O(n log n) for the sort, then O(n)

    Lesson
    What Makes Greedy Work · Greedy

Graphs, grids and trees

  • Breadth-first search

    Fewest steps in an unweighted graph or grid, or anything processed level by level.

    Signals

    • shortest path / minimum moves, all steps cost the same
    • level order, nearest, spreading minute by minute
    • several starting points at once (multi-source)

    Typical cost: O(V + E) time, O(V) space

    Lesson
    Breadth-First Search · Graphs
  • Depth-first search & flood fill

    Explore everything reachable — count regions, copy a structure, or check a property down every path.

    Signals

    • number of islands / connected regions
    • fill, paint, or mark a connected area
    • path sums and properties from root to leaf

    Typical cost: O(V + E) time, O(V) space

    Lesson
    Depth-First Search · Graphs
  • Topological sort

    Tasks with prerequisites: an order that respects every dependency, or proof that none exists.

    Signals

    • courses / builds / tasks with prerequisites
    • can all be finished? (cycle check)
    • an order derived from pairwise rules

    Typical cost: O(V + E) time, O(V) space

    Lesson
    Topological Sort · Graphs
  • Union-find

    Groups that only ever merge, and repeated "are these two connected?" questions.

    Signals

    • number of provinces / friend circles / components
    • the edge that closes a cycle (redundant connection)
    • merge accounts or items that share a key

    Typical cost: O(α(n)) per operation, amortized

    Lesson
    Disjoint Sets Basics · Union-Find
  • Weighted shortest paths

    Edges have different costs: Dijkstra for non-negative weights, Bellman–Ford when they can be negative or hops are limited.

    Signals

    • cheapest / fastest route, network delay
    • at most k stops
    • negative weights or negative-cycle detection

    Typical cost: O((V + E) log V) Dijkstra, O(V·E) Bellman–Ford

    Lesson
    Dijkstra's Algorithm · Graphs
  • Trie

    Many words and many prefix questions: autocomplete, starts-with, word search over a board.

    Signals

    • starts with / prefix / autocomplete
    • find all dictionary words in a grid
    • replace words by their shortest root

    Typical cost: O(L) per word, O(total characters) space

    Lesson
    Trie Basics · Tries

Recursion and dynamic programming

  • Backtracking

    You must list every valid arrangement, or search one under constraints: choose, recurse, un-choose.

    Signals

    • all subsets / permutations / combinations
    • place queens, fill a sudoku, find a word
    • n is small (≤ ~20)

    Typical cost: Exponential: O(2n) subsets, O(n!) orders

    Lesson
    The Decision Tree · Backtracking
  • 1-D dynamic programming

    The answer at position i depends on a few earlier answers, and plain recursion repeats work.

    Signals

    • number of ways to reach step n
    • max total without taking two neighbours
    • decode ways, min cost to climb

    Typical cost: O(n) time, O(1) with rolling variables

    Lesson
    Climbing Stairs · Dynamic Programming
  • Knapsack DP

    Pick items under a capacity or to hit a target, each item once (0/1) or any number of times (unbounded).

    Signals

    • fewest coins / number of ways to make an amount
    • split into two equal-sum halves
    • assign + / − to reach a target

    Typical cost: O(n·W) time, O(W) space

    Lesson
    0/1 Knapsack · Dynamic Programming
  • 2-D DP on strings and grids

    Two sequences compared prefix by prefix, or a grid where each cell is built from its neighbours.

    Signals

    • longest common subsequence, edit distance
    • unique paths / minimum path sum in a grid
    • palindromic subsequence

    Typical cost: O(m·n) time, O(n) with one row

    Lesson
    Longest Common Subsequence · Dynamic Programming

Bits

  • Bit tricks

    Pairs cancel, a set fits in an integer, or the question is about powers of two.

    Signals

    • every number appears twice except one
    • missing number in 0…n with O(1) space
    • power of two / count 1 bits / all subsets as masks

    Typical cost: O(n) time, O(1) space

    Lesson
    XOR Tricks · Bit Manipulation

bytepatterns.com/cheatsheets/patterns — every row links to an animated lesson there.

New lessons land every few weeks

Leave an address and we will tell you when the next one is up. That is the only reason we will use it.

One address, stored so we can email you. Nothing else, ever.