A written course

Learn the pattern, not the problem.

Interviewers reuse a small number of shapes. Once you can name the shape, the code mostly writes itself. This is a book about those shapes: what tells you which one you are looking at, why each one works, and where people reliably get them wrong.

Start with binary searchRead the chapter first, then work the seven problems in order.

How to read this

Each topic opens with a chapter of theory. Read it once, properly, before touching a problem. It builds the idea from its foundation rather than handing you a template to memorise, because a template you cannot derive is one you will misremember under pressure.

Every problem then follows the same six steps in the same order: how to recognise the pattern, the brute force and why it is not enough, the approach in plain language, the algorithm as a diagram, the solution with its reasoning, and a space for your own notes. The order never changes, so you learn the shape once and afterwards always know where to look.

Work the problems in the order given. They are arranged so each one adds a single idea to the one before it, and jumping to the interesting-looking ones is how people end up with gaps they cannot see.

Contents

4 of 18 patterns written, covering 31 problems. The remaining 119 are outlined in the roadmap.

Foundation

  1. Arrays & HashingoutlineDuplicates, membership, frequencies, anagrams, or “have I seen this before?”
  2. Two PointersoutlineA sorted sequence, a pair, a palindrome, or an answer improved by comparing two positions.
  3. StackoutlineNested structure, the most recent unresolved item, undo, expression evaluation, or next greater/smaller.

Core

  1. Binary SearchSorted data, a monotonic yes/no condition, or a request for the first/last feasible answer.
  2. Sliding WindowA contiguous subarray/substring with fixed k, longest valid, shortest valid, or at-most constraints.
  3. Linked ListNodes must be traversed, rewired, merged, split, cycled, or located from the end.

Traversal

  1. TreesHierarchy, subtree answers, paths, levels, ancestors, or ordered-tree constraints.
  2. TriesoutlinePrefix lookup, autocomplete, dictionary search, or many words sharing beginnings.
  3. GraphsoutlineRelationships, grids, connected components, dependencies, spreading, or unweighted shortest paths.

Decisions

  1. BacktrackingoutlineGenerate all combinations, permutations, partitions, placements, or paths under constraints.
  2. Heap / Priority QueueoutlineTop k, kth largest/smallest, repeated best choice, streaming rank, or merging sorted sources.
  3. 1-D DPoutlineRepeated subproblems where today’s best answer depends on a small set of earlier states.

Advanced

  1. Advanced GraphsoutlineWeighted shortest path, minimum spanning network, ordered itinerary, or unknown alphabet ordering.
  2. 2-D DPoutlineTwo strings, a grid, two indices, or decisions whose state needs two independent dimensions.
  3. GreedyoutlineA locally best choice may preserve all future possibilities or extend the reachable frontier.
  4. IntervalsoutlineRanges overlap, meetings conflict, a range must be inserted, or queries need the smallest covering range.
  5. Bit ManipulationoutlineXOR cancellation, flags, powers of two, binary counts, or arithmetic without normal operators.
  6. Math & GeometryoutlineCoordinate transforms, matrix layers, numeric cycles, exponentiation, or geometric state.

Written chapters

These have the full treatment. The rest are outlined in the roadmap and are still being written.