Skip to content

Pattern picker

Most interview problems give themselves away in a few words. “Sorted” suggests binary search or two pointers. “Shortest” plus “every move costs the same” suggests BFS. “Prerequisites” suggests a topological sort. Experienced candidates don’t recall solutions. They spot signals and test a short list of candidates against the constraints.

This page trains that step. Use it while you read a problem, before you write any code.

Tick everything the problem statement says, or implies. Candidates are ranked by how many signals point at them; the reasons explain the fit. A minus sign means a signal argues against a technique (negative values break the usual sliding window, for example).

Pattern picker
tick the signals
Try:
The input…
It asks for…
Constraints…
Domain words…
Candidates
Tick what the problem statement says. Candidates appear here, best first, with the reason each one fits.
  1. Read the problem twice. The first time for the story, the second time for signals. Underline words like sorted, contiguous, k-th, shortest, all, prerequisite, stream, interval.
  2. Read the constraints. n ≤ 20 allows exponential search. n ≤ 10⁵ needs O(n log n) or better. “Values change between queries” rules out a prefix array.
  3. Name two or three candidates out loud, with one reason each: “It’s contiguous with non-negative values, so a sliding window should work; if values can be negative I’d switch to prefix sums.”
  4. Test the leader on the example by hand before coding. If it fails on the example, the next candidate is usually the fix.
  5. Start with brute force if nothing fits. It’s a correct baseline, an oracle for testing, and its bottleneck often names the technique you need.

The same knowledge as a static table, grouped by where the signal shows up.

If the input… Think Because
is sorted (or can be sorted) Binary search, two pointers Order lets you discard half, or walk inward from both ends.
is about a contiguous subarray / substring Sliding window, prefix sums A window is one contiguous range; prefix sums answer any range in O(1).
has negative values and asks for subarray sums Prefix sums + hash map Windows need monotonic sums; prefix differences don’t.
is a grid or board Grids, BFS, DFS A grid is an implicit graph: neighbours are the four (or eight) adjacent cells.
is a tree or hierarchy Binary trees, DFS, recursion Solve each subtree, combine on the way up.
is nodes and edges Graph representation, BFS, DFS Build adjacency lists first; then traverse.
is a word list with prefix queries Tries, sorted containers A trie shares prefixes; bisect on sorted words finds a prefix range.
arrives as a stream Streaming, heaps, queues Keep a small summary, not the whole history.
is two strings compared position by position DP 2D A table over (prefix of A, prefix of B).
If it asks for… Think Because
k-th largest, top k, k closest Heaps A size-k heap gives O(n log k). Quickselect is O(n) expected.
fewest steps, all moves cost the same BFS, search over states BFS reaches nodes in order of distance.
cheapest path with different costs Shortest paths Dijkstra for non-negative weights; 0-1 BFS for weights 0 and 1.
all combinations / permutations / subsets Backtracking Choose, explore, un-choose, and prune.
count ways / min cost / max value, with repeated sub-questions DP 1D, memoization Solve each sub-question once.
pick items under a budget Knapsack Take-or-skip with remaining capacity as state.
smallest X that works / minimise the max Binary search on the answer Feasibility is monotone: if X works, X+1 works.
next greater / smaller element Monotonic stack Each element is pushed and popped once: O(n).
“seen before?”, pairs to a target, dedupe Hash maps and sets O(1) membership replaces a nested loop.
most frequent, anagrams Counting A frequency table is the whole trick.
floor / ceiling with inserts Ordered maps, sorted containers O(log n) predecessor and successor.
many range sums Prefix sums, segment trees Prefix sums if static; a segment or Fenwick tree if values change.
If you see… Think Because
intervals, meetings, bookings Intervals, sweep line Sort by start; or turn starts and ends into events.
dependencies, prerequisites, “A before B” Topological sort Orders a DAG and detects cycles.
groups that merge, “connected?” Union-find Near-constant union and find.
brackets, nesting, undo Stacks Last in, first out matches nesting.
expressions, commands, text formats Parsing, expression evaluation Tokenise first, then parse.
cache, capacity, evict least recently used Caching, linked lists A hash map plus an O(1) reorderable list.
expiry, TTL, “in the last N seconds” TTL and expiry, time handling Decide lazy vs eager expiry and the window boundaries.
rules applied tick by tick Simulation, state machines Model state exactly, then step it.
money, cents, rounding Money arithmetic Integer minor units and explicit rounding.
“as of time t”, history Versioning Per-key version lists plus binary search on time.
tasks, priorities, workers Scheduling, heaps Always run the best ready task.
“level 2 adds…” Adapting to changing requirements Design level 1 so later levels are additions.
n is about… Budget Usually means
≤ 12 O(n!) Permutations by backtracking.
≤ 20-25 O(2ⁿ) or O(2ⁿ·n) Subsets, bitmask DP.
≤ 500 O(n³) Interval DP, Floyd-Warshall.
≤ 5,000 O(n²) Pairwise DP, simple nested loops.
≤ 10⁵-10⁶ O(n log n) or O(n) Sorting, heaps, hash maps, two pointers, binary search.
≥ 10⁹ (a value, not a length) O(log n) Binary search on the answer, math.

These budgets assume roughly 10⁷-10⁸ simple operations per second, a common rule of thumb in competitive programming (see the complexity page). Python is slower per operation than C# or TypeScript, so aim one notch lower in Python.