Software Engineer (SDE)dsaintermediate3 interactive MCQs in app
Backtracking: Subsets, Permutations & N-Queens
choose-explore-unchoose state restorationpath[:] copy trap when recording resultsstart index vs used[] boolean array for subsets vs permutations
Core Concept
Backtracking explores a decision tree recursively via **Choose -> Explore -> Unchoose (`pop` / undo mutation)**, pruning invalid branches early instead of enumerating blind brute force. For `Subsets` (`O(n * 2^n)`), pass `start` index (`for i in range(start, len(nums))`) to avoid duplicate sets; when storing `path`, ALWAYS append a snapshot copy `res.append(path[:])`!
Worked Example
for `subsets([1, 2])`, choose `1` -> save `[1]`, choose `2` -> save `[1, 2]`, unchoose `2` (`pop()`), unchoose `1` (`pop()`), then choose `2` -> save `[2]`.
Common Pitfall
(classic!) doing `res.append(path)` without copying appends references to the SAME mutating list, ending up with `[[], [], [], ...]`!
Recommended Readings & Practice
Practice Backtracking: Subsets, Permutations & N-Queens with 3 graded MCQs & written feedback.
Take SOARDO’s free 12-question diagnostic to see how strong you are across your target role and unlock adaptive daily missions.