core practice · coding problem
Generate all subsets
Return all subsets of [1,2]. Use backtracking with include/exclude choice per index.
The mechanism
Backtracking is "try a choice, recurse, undo if it does not work, try the next one." It explores all possibilities, but you cut whole branches by stopping early when you can prove they will never reach a valid answer.
Expected evidence
[[],[1],[2],[1,2]] (order may vary).
Try this drill in the practice workspace →
Hints when you need them
- Push current path at each leaf.
- Undo (pop) after exploring.