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
- Practice
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
- Practice
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
- Practice
- Balance Point IndexEasy
- Product Of OthersMedium
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
- Practice
Binary search
The input is sorted — or a yes/no question about the answer flips exactly once as the answer grows.
Signals
- sorted (or rotated sorted) array, O(log n) expected
- first / last position, insert position
- minimum capacity / speed / days such that…
Typical cost: O(log n) per search; O(Clog R) on the answer
- Lesson
- Binary Search · Searching
- Practice
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
- Practice
- Loop in a ChainEasy
- Drop Nth From EndMedium
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
- Practice
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
- Practice
- K Weakest SquadsEasy
- Kth Largest ValueMedium
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
- Practice
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
- Practice
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
- Practice
- Attend Every MeetingEasy
- Car Pooling CapacityMedium
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
- Practice
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
- Practice
- Shallowest Leaf DepthEasy
- Right Edge ViewMedium
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
- Practice
- Open Every Locked RoomEasy
- Deep Copy A GraphMedium
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
- Practice
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
- Practice
- Reachable Pair CheckEasy
- Count ProvincesMedium
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
- Practice
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
- Practice
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
- Practice
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
- Practice
- Cheapest Stair ClimbEasy
- Decode Digit MessageMedium
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
- Practice
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
- Practice
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
- Practice
- The Missing ValueEasy
- Two Lone ValuesMedium
bytepatterns.com/cheatsheets/patterns — every row links to an animated lesson there.