Skip to content

Coding patterns

A coding round gives you one or two problems, and about 45 minutes. Almost none of those problems are new. They are variations on a small set of patterns. What the round really tests is whether you can spot the pattern from the problem statement, and adapt it. Then whether you can explain why it is correct, and how fast it runs. Grinding hundreds of problems teaches that slowly. Learning the patterns first, then practicing each one on purpose, teaches it fast.

This track covers the sixteen patterns that carry most of that weight. Each page explains how to recognize the pattern, and steps through it on a small input. It gives one template in five languages. And it ends with ten LeetCode problems, easy to hard, that you can tick off as you solve them.

Start with hashing and prefix sums

Patterns are easier to see than to read. Every page runs its algorithm on a small input, with the pseudo-code beside it. So you can watch the state change one step at a time. Here is two pointers finding a pair that sums to 10 in a sorted array, without trying every pair. Press Step, or Play to watch it run.

nums (target 10)
1031426384115
leftright
  1. left, right = 0, len(nums) - 1
  2. while left < right:
  3. pair_total = nums[left] + nums[right]
  4. if pair_total == target: return [left, right]
  5. if pair_total < target: left += 1 # need more
  6. else: right -= 1 # need less

Sorted input. Start at both ends.

1 / 6

The problem statement usually gives the pattern away. These are the phrases to listen for.

The problem says Try first
“Have we seen this before?”, pairs that sum to a target, counts, anagrams Hashing
Subarray sums, especially with negative numbers Prefix sums
A sorted array, pairs or triples, in-place rearranging, palindromes Two pointers
Longest or shortest contiguous substring or subarray that meets a rule Sliding window
Sorted input, or “the minimum X such that…” with a yes/no check Binary search
Matching brackets, “next greater” or “previous smaller” element Stacks
A tree: depth, paths, validating, level by level Tree traversal
Grids, connections, fewest steps, dependencies or prerequisites Graph search
The k largest, smallest, closest or most frequent; merging sorted lists Heaps
“All” combinations, subsets, permutations or placements Backtracking
“How many ways”, “fewest” or “most” with choices that overlap Dynamic programming
Reverse, merge or reorder nodes; find a cycle or the middle Linked lists
Ranges that overlap: merge, insert, rooms needed at once Intervals
A 2-D grid to walk in a spiral, rotate or mark in place Matrices
Count bits, the one number without a pair, add without + Bit manipulation
A best choice at each step that can never hurt later Greedy
Words sharing prefixes, autocomplete, many words on one board Tries

When two rows fit, start from the brute force and ask what work it repeats. The pattern is usually the thing that stops that repeat: a map stops a rescan, a window stops a recount, a table stops a recomputation.

160 problems

Tick a problem on its pattern page once you've solved it. Ticks stay in this browser; there's no account and nothing is sent anywhere.

The patterns build on each other in this order: arrays first, then trees and graphs, then recursion. The dots show the level: ●○○ Foundation, ●●○ Mid-level, ●●● Senior. The count next to each pattern is how many of its ten problems you’ve ticked.

  1. Arrays and strings

    Hash maps and prefix sums, two pointers, sliding windows, binary search and stacks turn an O(n²) scan into one pass. Then linked lists, intervals, matrices, bits and greedy choices.

    See all 10
  2. Trees, graphs and heaps

    Depth-first and breadth-first traversal, topological sort, the heap and the trie: how to walk a structure, find shortest paths, order dependencies, keep the best k items and look up prefixes.

    4 pages
  3. Backtracking and DP

    Searching every choice with backtracking, and remembering overlapping answers with dynamic programming: the two patterns behind most hard coding questions.

    2 pages
  4. Blind 75

    The 75 classic coding interview problems, each solved from the simple way to the fast way, with a step-through, code in five languages and the Big O worked out from the code.

    See all 75
  • Try each problem for 20 to 30 minutes before you look anything up. Getting stuck and then unstuck is the part that builds recognition.
  • Say the complexity out loud before you code. Interviewers ask. And saying it first often shows you the solution is too slow, before you have written it.
  • Come back to a problem a week later. If you can’t solve it again from a blank editor, you learned the answer, not the pattern. Untick it, and try again.