Free learning guide

Coding interview patterns: learn the shape behind the problem.

Patterns compress experience. They help you recognize familiar structure in a new prompt, choose a promising strategy, explain why it works, and avoid rebuilding your reasoning from zero.

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.

The reusable unit

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:

  1. Structure: Is the input sorted, contiguous, hierarchical, connected, or made of choices?
  2. Goal: Are you finding a pair, a best range, a shortest path, a top-k set, or a count of ways?
  3. 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

Two pointers

Useful positions move together through a sequence. Common signals: sorted pairs, partitioning, slow/fast movement, or one-pass compaction.

LEARN →
Sliding window

A contiguous range expands, shrinks, or moves while reusing the state shared with the previous range.

LEARN →
Binary search

Sorted order or a monotonic condition makes it safe to discard half of the remaining search space.

LEARN →
DFS and BFS

Traverse trees, graphs, grids, and state spaces deeply with a stack or evenly by distance with a queue.

COMPARE →
Backtracking

Build one candidate choice at a time, undo it, and prune branches that cannot become valid answers.

CHOOSE → EXPLORE → UNDO
Heap / priority queue

Repeatedly access the smallest or largest active item without fully sorting everything each time.

COMMON SIGNAL: TOP K
Greedy

Make a locally best safe choice and prove that revisiting earlier choices is unnecessary.

PROOF MATTERS
Dynamic programming

Define a state, connect it to smaller states, and store answers when subproblems overlap.

STATE + TRANSITION

Pattern signals are hypotheses, not rules

Prompt cluePattern to testQuestion to ask
Sorted pair or tripleTwo pointersDoes the comparison tell me which side cannot help?
Longest/shortest contiguous rangeSliding windowCan I update the range state as a boundary moves?
Sorted or monotonic search spaceBinary searchCan one test prove that half the candidates are impossible?
Connected region or all pathsDFSShould I finish one branch or component before the next?
Minimum steps with equal-cost edgesBFSDoes processing one distance layer at a time guarantee the first answer?
Count/best answer across repeated statesDynamic programmingDo 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.

  1. Read the prompt and say the likely pattern out loud.
  2. Name the invariant before writing a loop or recursion.
  3. Write a language-neutral outline: initialize, inspect, update, stop, return.
  4. Test one normal case and one boundary case by hand.
  5. Code, then explain the complexity from the work performed.
  6. Revisit mistakes after time has passed, not only while the answer is fresh.
Inside Cozytek Code

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.

Practice on iPhone

Turn pattern recognition into a repeatable skill.

Use Cozytek Code for visual lessons, guided practice, solution outlines, and private mastery tracking.

Download on the App Store