LESSON 25 / 75·2 min readIntermediate

Recursion and backtracking in JavaScript

Should knowConcept lesson
01 Understand02 Recall03 Prepare

Problems to practice

  • Fibonacci
  • Permutations
  • Subsets
  • Combination Sum
  • N-Queens
  • Sudoku

Example: permutations

JavaScript
function permute(nums) {
  const result = [];

  function backtrack(path, remaining) {
    if (remaining.length === 0) {
      result.push([...path]);
      return;
    }

    for (let i = 0; i < remaining.length; i++) {
      const val = remaining[i];
      path.push(val);
      const next = remaining.filter((_, idx) => idx !== i);
      backtrack(path, next);
      path.pop();
    }
  }

  backtrack([], nums);
  return result;
}

Interview guidance

Backtracking means trying a branch and undoing the choice when it fails.


PAUSE & RECALL

Can you explain it without the notes?

Recursion and backtracking in JavaScript

Use your own words. Explain an example and one limitation before checking the explanation.

Check your explanation

Common questions

CONNECT IT TO YOUR INTERVIEW

Turn this concept into a clear answer.

Practice this concept
YOUR NEXT STEP

Keep connecting the ideas.

Next lesson: Trees and BST in JavaScript

Adapted from 16_DSA_JAVASCRIPT.md · “6. Recursion and backtracking in JavaScript”. Examples are study fragments; framework behavior can depend on your version.