Problem-Solving Patterns

About 35 patterns cover the large majority of interview problems. Each lesson shows how to spot the pattern, why it works, one reusable template and a practice ladder.

What you will learn

  • Recognise the pattern from the wording and the constraints of a problem
  • Reuse one template per pattern instead of memorising hundreds of solutions
  • Explain why each pattern is correct and what it costs
  • Avoid the classic off-by-one, overflow and edge-case traps
  • Climb from easy to hard problems in every pattern with a curated ladder

Arrays & Strings

The most common interview category. Five patterns turn most O(n²) brute forces into O(n).

  • Hash Maps & Sets: Trade memory for time: "have I seen this?", counting, and grouping in O(1).
  • Two Pointers: Two indexes moving toward each other or in the same direction to avoid nested loops.
  • Sliding Window: Grow the window, shrink when it breaks the rule — for subarrays and substrings.
  • Prefix Sums & Difference Arrays: Answer range-sum questions in O(1) and count subarrays with a hash map.
  • Intervals: Sort by start, then merge, insert or sweep — meeting rooms and overlaps.

Searching & Sorting

Binary search is far more than finding a number in a sorted array, and an array of 1 to n can sort itself.

  • Binary Search: One bug-free template: find the first position where a condition becomes true.
  • Binary Search on the Answer: When the answer is a number and "can I do it with X?" is monotonic.
  • Sorting as a Tool: Custom comparators, counting sort and quickselect for the k-th element.
  • Cyclic Sort: Values 1 to n each have a home index: find missing and duplicate numbers in O(n) time and O(1) space, and count minimum swaps.

Linked Lists

Two techniques solve almost every linked-list question.

Stacks & Queues

Last-in-first-out for matching and parsing; monotonic stacks for "next greater".

Heaps

When you need the smallest or largest quickly, again and again.

Trees

Think in subtrees: most tree problems are three lines of recursion.

Graphs

Grids are graphs too. Four patterns cover nearly every graph interview question.

  • Graphs & Grids: BFS and DFS: Islands, flood fill, shortest steps in a grid and multi-source BFS.
  • Topological Sort: Order tasks with dependencies and detect cycles (Kahn's algorithm).
  • Union-Find: Group things that are connected, fast — with path compression.
  • Shortest Paths: BFS for unweighted, Dijkstra for weighted, 0-1 BFS for in between.

Backtracking

Choose, explore, un-choose — one template for subsets, permutations and puzzles.

  • Backtracking: Subsets, combinations, permutations and constraint puzzles with one template.

Dynamic Programming

DP is recursion plus memory. Learn to find the state and the rest follows.

  • DP Without Fear: From brute-force recursion to memoisation to a table, in four mechanical steps.
  • 1D DP: Climbing stairs, house robber, maximum subarray and longest increasing subsequence.
  • Knapsack Family: Take it or leave it: subset sum, coin change and their variants.
  • Grid DP: Paths and costs on a grid, and how to save memory with one row.
  • String DP: Two strings, two indexes: LCS, edit distance and palindromes.
  • Tree & Interval DP: DP on subtrees (house robber III, cameras) and on ranges (burst balloons, cutting sticks).
  • State-Machine & Bitmask DP: Stock problems as a few states per day, and subsets of a small set as the state.

Greedy & Special Topics

When a local choice is safe, plus two data structures that appear often enough to learn.

  • Greedy: Make the best local choice — and how to convince yourself it is safe.
  • Tries: Prefix trees for autocomplete, word search and prefix counting.
  • Bit Manipulation: XOR tricks, masks and counting bits — the handful that interviews use.

Design, Matrices & Advanced Topics

The remaining interview favourites: design-a-data-structure, matrix simulation, maths, string matching and range queries.

Problem-Solving Patterns cheat sheet