a. algoviz / study order
TS THE INTERVIEW FIELD GUIDE 01 / ALGORITHMS

Recognize the pattern.
Know the next move.

The minimum to recognize, explain, and implement the common interview patterns. All templates are TypeScript. Open a pattern when you need the details.

22 patterns15 core firstRunnable templatesLinked visualizations

Before you reach for a template

  1. 01
    Clarify

    Input size, duplicates, ordering, mutation, and the exact output.

  2. 02
    Explain

    State a brute-force solution, then what repeated work you can remove.

  3. 03
    Check

    Trace one example. Test empty, singleton, ties, and boundary cases.

START WITH THE CLUE

What is the problem asking?

Clues, not guarantees.

Find a pair or remember a count

Work with a contiguous range

Search sorted data or a yes/no boundary

Explore choices or reuse subproblems

THE REFERENCE

Patterns to keep in reach

Costs describe the shown template.

22 of 22 patterns · V = vertices, E = edges, h = tree height

Remember earlier work so each lookup replaces another scan.

Know this
Use Set for membership and Map for counts or indices. Decide exactly what each key represents.
Keep true
Before processing index i, the map contains only earlier indices.
Watch for
Look up the complement before inserting the current value, or you can reuse the same index. Check undefined, not truthiness: index 0 is valid.
Time O(n) expectedSpace O(n)
TypeScript
function twoSum(a: number[], target: number): number[] {
  const seen = new Map<number, number>();
  for (let i = 0; i < a.length; i++) {
    const j = seen.get(target - a[i]!);
    if (j !== undefined) return [j, i];
    seen.set(a[i]!, i);
  }
  return [];
}

TRACE ITtwoSum([2, 7, 11, 15], 9) → [0, 1]

LANGUAGE CHECK

TypeScript details worth remembering

TS

Numbers need a comparator

[...a].sort((x, y) => x - y)

Default sort compares strings; sort mutates its array. Copy if the input must stay unchanged.

Use a queue head

const item = queue[head++];

Keep push for enqueue. Repeated shift can move every remaining element. A head index retains consumed slots, so this queue uses O(total enqueued) storage.

Zero is a value

count.set(x, (count.get(x) ?? 0) + 1);

Check map.has(key) or value !== undefined. A stored index or count of 0 is not missing. Map and Set compare objects by identity.

Each matrix row needs its own array

Array.from({length: rows}, () => Array<number>(cols).fill(0))

Using fill with a single inner array makes every row share the same object.

Know your integer range

Number.MAX_SAFE_INTEGER // 2 ** 53 - 1

Use bigint if exact integer results can exceed this range. Number bitwise operations use 32-bit values. BigInt arithmetic has a cost that grows with its bit length.

Be precise about strings and !

const characters = [...text];

This iterates Unicode code points; text[i] uses UTF-16 code units. Neither handles every visible grapheme. In templates, ! asserts an index already proved valid; it adds no runtime check.

Complexity shorthand assumes constant-cost numeric operations and expected O(1) hash lookups. Include sorting, copied arrays, the recursion stack, and returned output when explaining your costs.

Pass (a, b) => a < b for a min-heap or (a, b) => a > b for a max-heap. Array indices: parent ⌊(i − 1) / 2⌋, children 2i + 1 and 2i + 2.

You know a pattern when you can explain why the next step is safe.

Templates: TypeScript · linked visualizations: JavaScriptBack to top ↑