What is a coding interview pattern?
A coding interview pattern is a reusable way to organize the state and movement in a family of problems. Two pointers is not one solution to one array question. It is a way to coordinate two positions so that each comparison safely removes work. BFS is not one tree traversal. It is a way to process states by distance using a queue.
That distinction matters because interview prompts rarely announce the intended pattern. You infer it from the data, the constraints, the relationship being asked for, and the kind of progress a valid solution can make.
Memorize less code. Practice more recognition, invariants, state, movement, termination, and complexity.
How do you recognize the right pattern?
Start by describing the problem without implementation details. Then look for three kinds of evidence:
- Structure: Is the input sorted, contiguous, hierarchical, connected, or made of choices?
- Goal: Are you finding a pair, a best range, a shortest path, a top-k set, or a count of ways?
- Progress: Can each step eliminate a side, slide a boundary, finish a branch, expand a frontier, or reuse a solved state?
A keyword can be a useful clue, but it is not proof. “Shortest” often suggests BFS only when edges have equal cost. “Sorted” makes binary search possible, but a prompt may still call for two pointers. The invariant—the fact that stays true after each move—is the stronger test.
A practical coding interview pattern map
Useful positions move together through a sequence. Common signals: sorted pairs, partitioning, slow/fast movement, or one-pass compaction.
LEARN → Sliding windowA contiguous range expands, shrinks, or moves while reusing the state shared with the previous range.
LEARN → Binary searchSorted order or a monotonic condition makes it safe to discard half of the remaining search space.
LEARN → DFS and BFSTraverse trees, graphs, grids, and state spaces deeply with a stack or evenly by distance with a queue.
COMPARE →Build one candidate choice at a time, undo it, and prune branches that cannot become valid answers.
CHOOSE → EXPLORE → UNDORepeatedly access the smallest or largest active item without fully sorting everything each time.
COMMON SIGNAL: TOP KMake a locally best safe choice and prove that revisiting earlier choices is unnecessary.
PROOF MATTERSDefine a state, connect it to smaller states, and store answers when subproblems overlap.
STATE + TRANSITIONPattern signals are hypotheses, not rules
| Prompt clue | Pattern to test | Question to ask |
|---|---|---|
| Sorted pair or triple | Two pointers | Does the comparison tell me which side cannot help? |
| Longest/shortest contiguous range | Sliding window | Can I update the range state as a boundary moves? |
| Sorted or monotonic search space | Binary search | Can one test prove that half the candidates are impossible? |
| Connected region or all paths | DFS | Should I finish one branch or component before the next? |
| Minimum steps with equal-cost edges | BFS | Does processing one distance layer at a time guarantee the first answer? |
| Count/best answer across repeated states | Dynamic programming | Do different decision paths ask for the same smaller answer? |
How to practice patterns effectively
After solving a problem, do not only save the final code. Record the clues you missed, the invariant you needed, the state you tracked, the reason each move was safe, and the final time and space costs. Those notes are more transferable than the exact implementation.
- Read the prompt and say the likely pattern out loud.
- Name the invariant before writing a loop or recursion.
- Write a language-neutral outline: initialize, inspect, update, stop, return.
- Test one normal case and one boundary case by hand.
- Code, then explain the complexity from the work performed.
- Revisit mistakes after time has passed, not only while the answer is fresh.
Visual lessons, guided solution outlines, pattern-focused mastery checks, custom questions, and spaced review are built around this learning loop.
Common questions
How many patterns should I learn?
Learn a small set deeply before collecting many names. Arrays and hash maps, two pointers, sliding window, binary search, tree and graph traversal, heaps, backtracking, greedy reasoning, and dynamic programming cover a large part of common interview practice.
Should I memorize templates?
A short outline can reduce cognitive load, but it should follow understanding. If you cannot explain why the boundary moves or why a state is sufficient, the template is fragile.
What should I do when two patterns seem possible?
Compare the invariant and complexity. Ask what each approach stores, what progress each move guarantees, and whether the input structure actually supports the elimination you want.