Part 10 · 2 chapters · ~12 min

Interview Patterns

The recurring shapes behind coding interview problems (two pointers, sliding window, binary search on the answer, BFS and DFS, backtracking, heaps, DP, union-find), how to recognise each from the problem statement, a method for the interview itself, and a practice plan.

17

Shapes, not tricks

code
// sliding window: longest substring without repeating characters, O(n)
function longestUnique(s: string): number {
  const last = new Map<string, number>(); let best = 0, left = 0;
  for (let right = 0; right < s.length; right++) {
    const seen = last.get(s[right]); if (seen !== undefined && seen >= left) left = seen + 1;
    last.set(s[right], right); best = Math.max(best, right - left + 1);
  }
  return best;
}
// binary search on the answer: smallest daily capacity that ships all packages within D days
function shipWithinDays(w: number[], D: number): number {
  let lo = Math.max(...w), hi = w.reduce((a, b) => a + b, 0);
  const days = (cap: number) => { let d = 1, cur = 0; for (const x of w) { if (cur + x > cap) { d++; cur = 0; } cur += x; } return d; };
  while (lo < hi) { const mid = (lo + hi) >> 1; if (days(mid) <= D) hi = mid; else lo = mid + 1; }
  return lo;
}
INTERVIEW PATTERNS
reusable shapes that solve most coding problems
two pointersSorted arrays, pairs summing to atarget, removing duplicates inplace.sliding windowLongest/shortest subarray with aproperty; expand right, shrinkleft.binary search on the answer"Smallest capacity that ships in Ddays": search the answer space.BFS / DFSGrids, trees, graphs, shorteststeps, islands, dependencies.backtrackingPermutations, subsets, constraintsearch with pruning.heap / DP / union-findTop-k, overlapping subproblems,connectivity.
swipe the figure sideways, or tap expand for full screen
1/6
two pointers
Two indices moving toward each other (or the same way) over a sorted array solve pair and partition problems in O(n) instead of O(n²).
two indices, one passsorted input is the hint
18

The interview method and a practice plan

in the interview
  1. Restate the problem and ask about input sizes and edge cases (empty, duplicates, negatives, huge n).
  2. Work two small examples by hand, including an edge case.
  3. Say the brute force and its complexity, then name the pattern that improves it.
  4. Write clean code, narrating; test with your examples; then the edge cases.
  5. State the final time and space complexity.

Practice plan: 8-10 problems per pattern, mixed easy and medium, untimed first, then timed (30-40 minutes). After each, write one line: "the hint was X, the pattern was Y". That line is what you are actually training.