Coding Interview Patterns

Recognising which of the common problem-solving patterns an unseen coding problem needs, and solving it in Python, Java or TypeScript within the time an interview allows.

  • 17 topics
  • 135 cards
  • 70 questions
  • Intermediate to Advanced
Share

Approach

How to start any problem, and the hash map, the first tool most optimisations reach for.

  • Solving an unseen problem: a fixed sequence of steps from clarifying the input to tracing the code, so a coding problem never starts from a blank page.

    Intermediate to Advanced8 cards · 5 qnot started

  • Hash map patterns: complements, counts and canonical keys that turn a search inside a loop into one lookup, and the array used as its own hash map.

    Intermediate to Advanced8 cards · 4 qnot started

Array and Number Patterns

Patterns over arrays, strings and the bits of a number: pointers, windows, running totals and stacks.

  • Two pointers: walking a sorted array from both ends, or rewriting it in place with a read and a write pointer, and the precondition each needs.

    Intermediate to Advanced8 cards · 4 qnot started

  • Sliding window: keeping a running answer for a contiguous run as it slides, with a fixed size or one that grows and shrinks, and the inputs that break it.

    Intermediate to Advanced8 cards · 5 qnot started

  • Prefix sums: precomputing running totals so any range sum is one subtraction, and counting the runs that hit a target with a hash map.

    Intermediate to Advanced8 cards · 4 qnot started

  • Monotonic stack: finding the next greater or smaller element of every position in one pass, with a stack kept in order.

    Intermediate to Advanced8 cards · 4 qnot started

  • Bit manipulation: reading and changing single bits with AND, OR, XOR and shifts, and where Python, Java and TypeScript give different answers.

    Intermediate to Advanced8 cards · 4 qnot started

Linked List Patterns

Pointer moves on linked lists: two speeds to find a middle or a cycle, and reversal in place.

  • Fast and slow pointers: two pointers moving at different speeds that find a list's middle, detect a cycle and locate where the cycle starts.

    Intermediate to Advanced8 cards · 4 qnot started

  • Linked list reversal: reversing a linked list in place with three references, and reversing part of a list or groups of nodes with a dummy node.

    Intermediate to Advanced8 cards · 4 qnot started

Sorting, Searching and Priority

Patterns that sort first, search on a condition, or keep the best items in a heap.

  • Intervals: merging, inserting and intersecting ranges after sorting them by start, and counting the most that overlap at once.

    Intermediate to Advanced8 cards · 4 qnot started

  • Modified binary search: binary search on a condition instead of a value, on a rotated array, and over the possible answers to a problem.

    Intermediate to Advanced8 cards · 4 qnot started

  • Heap patterns: keeping the top K items, merging sorted lists and tracking a running median with one heap or two.

    Intermediate to Advanced8 cards · 4 qnot started

Tree and Graph Patterns

Templates for trees, grids and the graphs hidden in a problem's description.

  • Tree patterns: solving binary tree problems by passing values down or returning them up, level by level, or through both subtrees at once.

    Intermediate to Advanced8 cards · 4 qnot started

  • Grid traversal: treating a grid of cells as a graph for breadth-first and depth-first search, flood fills and searches from many starting cells.

    Intermediate to Advanced8 cards · 4 qnot started

  • Graph modelling: finding the vertices and edges hidden in a problem's wording, then solving it with a search, a topological sort or union-find.

    Intermediate to Advanced8 cards · 4 qnot started

Choosing

Making a working solution faster, and choosing the pattern for a problem from its cues.

  • Optimising a solution: finding the step that dominates a solution's cost and removing it with a standard move, and knowing when to stop.

    Intermediate to Advanced8 cards · 4 qnot started

  • Choosing a pattern: reading the cues in a coding problem's wording to pick the pattern that fits, and telling a real cue from a keyword.

    Intermediate to Advanced7 cards · 4 qnot started