Skip to content
Work Practice Products Coding interviews Machine learning coding Quant developer HFT course Blog Method Why me About Book a call

Practice · Coding

Which pattern?

Most candidates can implement the patterns and still freeze, because nothing in the question says which one to use. This is the decision tree to run in the first two minutes, out loud.

Read the question. What is it asking for?
a number a structure
Can you check a candidate answer quickly, and is the check monotone?
Is the answer a contiguous run, a pair, or a whole arrangement?
yes no
Binary search on the answer
Do subproblems repeat?
yes
Dynamic programming
contiguous run pair or ends
Sliding window
Sorted input?
yes
Two pointers
no
Sort, or hash map
The current best of a changing set, again and again? A heap. Every combination, placement or split? Backtracking. Each element needs the nearest larger or smaller one, or a matching partner seen earlier? Use a stack. If two branches fit, write the brute force first and say out loud which one you are testing. The pattern is a starting point, not the answer: the interviewer is listening for why you chose it.

The four questions, in order

  1. Is the answer a number?"Minimum capacity", "smallest k", "maximise the minimum". If you can check a candidate faster than you can build one, and the check is monotone, binary search the answer.
  2. Is it a contiguous run?"Longest substring such that", "subarray with sum at most". Grow a window on the right, repair it from the left, and never move the left edge back.
  3. Is the input sorted, or about pairs and ends?Two pointers, provided you can argue that a comparison rules one end out for ever. If you cannot, sort first or use a hash map.
  4. Do subproblems repeat?Write the recursion. If it asks for the same arguments twice, it is dynamic programming: choose an order that makes each entry final before it is read.

Then say which one you picked and why, before you type. That sentence is worth more marks than the first ten lines of code.


The patterns

0116 problems

Arrays and hashing

Trade memory for time: a hash map answers "have I seen this?" in O(1), and most array problems reduce to asking it the right question.

Learn the pattern →
0226 problems

Two pointers

1 3 4 5 7 11 lo hi too small → lo++ too big → hi-- each comparison removes one index for good

Two indices walking a sorted or paired structure, each step ruling out one candidate for good.

Learn the pattern →
0320 problems

Sliding window

t m m z u x t lo jumps past the repeat hi always moves right the window is valid at every step

A window that grows on the right, shrinks on the left, and holds an invariant at all times.

Learn the pattern →
0429 problems

Binary search

not enoughworks first feasible answer lo and hi close in, halving each time check is monotone: once true, always true

Search the answer, not the array: halve a monotone predicate until one candidate is left.

Learn the pattern →
0549 problems

Dynamic programming

0 1 2 1 1 2 2 0123456 best[6] = best[3] + 1 every smaller amount is already final fill in an order that makes each entry final

Write the recursion, notice the repeats, fill a table in an order that makes each entry final.

Learn the pattern →
068 problems

Stack

73 74 75 71 69 72 69 71 75 stack (top) 72 pops 69 and 71: their answer is today each index is pushed once and popped once

Keep the elements whose question is still open; each new element settles the ones it beats and waits its turn.

Learn the pattern →
073 problems

Heap (priority queue)

3 5 8 9 7 12 smallest on top each parent ≤ its children; push and pop are O(log n)

Keep the few elements that matter in a structure that hands you the smallest (or largest) in O(1) and updates in O(log n).

Learn the pattern →
083 problems

Backtracking

✕ fails: cut solution dead end choose, explore, undo

Build a solution one choice at a time, and abandon a branch the moment it cannot succeed.

Learn the pattern →
094 problems

Intervals

merged block merged block gap sort by start; a start after the block ends opens a new block

Sort by start to merge and count overlaps; sort by end to choose. One sorted pass replaces checking every pair.

Learn the pattern →
1037 problems

Graphs

212341012321234 each ring is one step further: the first visit is the shortest path

Grids, networks and dependencies are all graphs. Breadth-first search finds shortest paths, depth-first search and union-find find what is connected, Dijkstra handles weighted edges, and a queue of nodes with no remaining prerequisites gives an order.

Learn the pattern →