Backtracking is a structured way to explore all valid combinations, permutations, or assignments.

The base pattern is simple:

  1. Choose a candidate
  2. Explore recursively
  3. Undo the choice

Interview difficulty comes from:

  • Designing state correctly
  • Avoiding duplicates
  • Pruning early without skipping valid answers

Core template

function backtrack(
  state: number[],
  choices: number[],
  used: boolean[],
  result: number[][],
): void {
  if (state.length === choices.length) {
    result.push([...state]);
    return;
  }

  for (let i = 0; i < choices.length; i++) {
    if (used[i]) continue;
    used[i] = true;
    state.push(choices[i]!);
    backtrack(state, choices, used, result);
    state.pop();
    used[i] = false;
  }
}

This solves permutations.

Example: subsets (power set)

Subsets are a clean "take or skip" DFS.

export function subsets(nums: number[]): number[][] {
  const result: number[][] = [];
  const path: number[] = [];

  const dfs = (index: number) => {
    if (index === nums.length) {
      result.push([...path]);
      return;
    }

    dfs(index + 1); // skip

    path.push(nums[index]!); // take
    dfs(index + 1);
    path.pop();
  };

  dfs(0);
  return result;
}

Example: combination sum with pruning

Given candidates and target, find combinations that sum to target. Each candidate can be reused.

export function combinationSum(candidates: number[], target: number): number[][] {
  candidates.sort((a, b) => a - b);
  const result: number[][] = [];
  const path: number[] = [];

  const dfs = (start: number, remain: number) => {
    if (remain === 0) {
      result.push([...path]);
      return;
    }

    for (let i = start; i < candidates.length; i++) {
      const value = candidates[i]!;
      if (value > remain) break; // pruning
      path.push(value);
      dfs(i, remain - value); // reuse allowed
      path.pop();
    }
  };

  dfs(0, target);
  return result;
}

Sorting enables early stop when value > remain.

Effective pruning strategies

  • Bound pruning: stop when sum exceeds target or remaining budget cannot fit
  • Duplicate pruning: skip identical values at same recursion depth
  • Constraint pruning: reject partial state that already violates constraints
  • Order pruning: sort first so you can break early

Pruning must preserve correctness. You need proof that skipped branches can never produce valid answers.

Avoiding duplicates

For "combination" style problems with repeated values, use:

if (i > start && nums[i] === nums[i - 1]) continue;

That skips duplicates at the same depth, not across different levels.

Complexity discussion

Backtracking is often exponential in worst case. In interviews, explain:

  • Branching factor
  • Depth
  • Effect of pruning on practical runtime

Clear complexity reasoning is often the difference between "works" and "strong answer."

Practice set

  • Subsets
  • Permutations
  • Combination sum
  • N-Queens
  • Word search
  • Palindrome partitioning

Backtracking becomes reliable when you treat it as a disciplined framework, not ad hoc recursion.