Recursion and backtracking in JavaScript
Problems to practice
- Fibonacci
- Permutations
- Subsets
- Combination Sum
- N-Queens
- Sudoku
Example: permutations
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.
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
Turn this concept into a clear answer.
Practice this conceptAdapted from 16_DSA_JAVASCRIPT.md · “6. Recursion and backtracking in JavaScript”. Examples are study fragments; framework behavior can depend on your version.