RavnaryA Knowledge Platform

Cheatsheet

Coding Interview Patterns

Fourteen coding interview patterns on one page: the property that points to each, what it costs, when it fails, and its loop in Python.

ravnary.com/cheatsheets/coding-interview-patterns · Checked against Python 3.14, Java 25 and TypeScript 5.9 on Node 24.

  • 14 entries
  • 4 groups

Checked against

Python 3.14, Java 25 and TypeScript 5.9 on Node 24.
Share

Scope

Every pattern in the subject; a problem none fits goes to the six steps of Solving an Unseen Problem, and graph algorithms to their own sheet.

The rule

A pattern is chosen by a property the problem has, never by a word it uses: the input's structure first, then the question, then the values.

Choosing

14 choices
  1. When A linked list, and the question asks about a loop, the middle, or a position counted from the end

    , choose

    Fast and Slow Pointers

  2. When A linked list must be reversed or reordered, whole, in a stretch or in groups, without a copy

    , choose

    Linked List Reversal

  3. When A tree, and the answer at a node comes from the path above it, the subtree below it, or its level

    , choose

    Tree Patterns

  4. When A map of cells, and movement goes between cells that touch

    , choose

    Grid Traversal

  5. When Things linked in pairs with no graph given, and a question about fewest steps, an order or groups

    , choose

    Graph Modelling

  6. When Each item has a start and an end, and the question asks about overlaps, gaps, or how many are open at once

    , choose

    Intervals

  7. When The k best items from anywhere in the input, the next across sorted sources, or a running middle

    , choose

    Heap Patterns

  8. When The answer is where a yes-or-no test turns from false to true, over positions or possible answers

    , choose

    Modified Binary Search

  9. When For each item, the nearest item on one side that is larger or smaller

    , choose

    Monotonic Stack

  10. When The best contiguous run of a fixed length, or of any length when growing it moves the rule one way

    , choose

    Sliding Window

  11. When Sums of contiguous runs when values can be negative, or many range sums over data that never changes

    , choose

    Prefix Sums

  12. When Sorted input and a question about pairs or a split, or three or more items that meet a target

    , choose

    Two Pointers

  13. When Each item is a few yes-or-no flags, or every value appears twice but one, in constant memory

    , choose

    Bit Manipulation

  14. When Single items in unsorted input, such as one seen before, a pair's partner, or counts, with memory to spare

    , choose

    Hash Map Patterns

Arrays and numbers

6 entries

Each answers a question over a sequence of values in about one pass, and they differ in what the values must allow.

  • Hash Map Patterns

    A map of the items seen so far answers each item's question in one lookup: O(n) on average.

    Avoid when

    Memory must stay constant, or the input is sorted and two pointers find the pair without a map.

  • Two Pointers

    Move one of two pointers on a sorted array, each move ruling out a row of pairs: O(n), worst case.

    Avoid when

    The input is unsorted and cannot be sorted first; a hash map of complements answers instead.

  • Sliding Window

    Grow a run on the right and shrink it from the left while it breaks the rule: O(n), worst case.

    Avoid when

    Values can be negative, so dropping one from the left can raise the sum; prefix sums answer instead.

  • Prefix Sums

    Running totals make any range sum one subtraction: O(n) to build and O(1) per range, every case.

    Avoid when

    The values change between questions, since every later total must then be rebuilt.

  • Monotonic Stack

    A stack of unanswered positions; each new value answers every smaller one on top: O(n), worst case.

    Avoid when

    The question asks for the largest value anywhere, not the nearest larger one on one side.

  • Bit Manipulation

    Read an integer as a row of switches: AND tests, OR sets, AND NOT clears, and XOR cancels pairs.

    Avoid when

    The flags outnumber the bits: 32 in a Java int and in TypeScript's operators, 64 in a Java long.

Linked lists

2 entries

Both walk a linked list once with constant extra memory: one finds a position, and the other reorders the nodes.

  • Fast and Slow Pointers

    Slow steps one node and fast two; they meet in a cycle, or slow ends mid-list: O(n), worst case.

    Avoid when

    The input is an array, where an index finds the middle, or memory allows a set of the nodes seen.

  • Linked List Reversal

    Save the next node, point the current one backwards, then step both on: O(n), every case.

    Avoid when

    A copy is allowed, or the original order is still needed after the reversal.

Sorting, searching and priority

3 entries

Each relies on an order: intervals sorted by start, a test that turns true once, or a heap ordered by size.

  • Intervals

    Sort by start, then merge each interval into the last one when they overlap: O(n log n), worst case.

    Avoid when

    The times are whole units in a small range, where an array with one slot per unit is simpler.

  • Modified Binary Search

    Halve the candidates down to the first that passes a yes-or-no test: O(log n) tests, worst case.

    Avoid when

    The test changes between false and true more than once, so there is no single boundary.

  • Heap Patterns

    A min-heap capped at k holds the k largest so far, the weakest on top: O(n log k), worst case.

    Avoid when

    The k items must form one contiguous run, which is a sliding window, not a heap.

Trees, grids and graphs

3 entries

Each walks linked things, and they differ in whether the links are given, implied by a grid, or still to be found.

  • Tree Patterns

    Pass what the path knows down as an argument, and return what the subtree found: O(n), every case.

    Avoid when

    A node can be reached two ways, so the structure is a graph and needs a visited set.

  • Grid Traversal

    Each cell is a vertex and its four neighbours its edges, never stored: O(R · C), worst case.

    Avoid when

    Moves cost different amounts, such as road and mud, where Dijkstra's algorithm replaces the queue.

  • Graph Modelling

    Decide the vertex, the edge, its direction and its weight; then the question names the algorithm.

    Avoid when

    The links are already given as a list, tree or grid, whose own pattern applies directly.

Side by side

1 table
Four array patterns on the same axes
PatternNeedsQuestion it answersCostBreaks when
Two pointersSorted inputPairs, or a split of the arrayO(n) after any sortThe input is unsorted and sorting would lose what is asked, such as positions
Sliding windowA fixed length, or a condition that moves one way as the run growsThe best contiguous runO(n)Values can be negative, so a longer run can have a smaller sum
Prefix sumsData that does not change between questionsRange sums; runs with a given sum, with a hash map of earlier totalsO(n) to build, O(1) a questionValues change between questions
Binary searchA test that changes once, from false to trueThe boundary where it changesO(log n) testsThe test changes more than once

Common mistakes

7 mistakes
  1. Choosing a pattern from a word in the question, as largest suggests a heap, instead of a property the problem has.

  2. Recording an item before looking up its complement, so 6 pairs with itself when the target is 12.

  3. Reading the range from i to j as p[j] − p[i], which drops the last item; it is p[j + 1] − p[i].

  4. Storing values on a monotonic stack when the answer is a distance, which fails once a value repeats.

  5. Merging intervals sorted by end, so a long one that starts early arrives last and joins the wrong neighbour.

  6. Keeping the k largest in a max-heap capped at k, which throws the best item away and keeps the k smallest.

  7. Returning the path that bends through a node as its height, so the parent joins two branches that cannot meet.

Learn it

14 topics