Count the subsets of a three-element set.
Acceptance criteria
- 1.#out should show eight subsets.
DSA Tutorial · Backtracking
These 10 tasks go with the Backtracking Explained chapter. 5 of them ask you to build something small from scratch; 5 give you code that is already broken and ask you to work out why. Set aside about 90 minutes for the whole set, though you can do them in any order.
Nothing here is marked by a person. When you press Submit, the editor checks your code against the points listed under each task and tells you straight away what passed and what didn't (you need a free account to submit). If you get stuck, there are hints, and a worked solution once you've had a go. The first 2 tasks are free; the rest are part of the practice plan.
If it's been a while since you read the chapter, here is how it starts: “Backtracking is brute force with an eraser. You try a choice, follow it, and if it leads nowhere you undo it and try the next — which is why the undo step is the part that matters.” .
1.All subsetsexercise · easy · 10 min
Count the subsets of a three-element set.
Passes when: #out should show eight subsets.
2.Permutationsexercise · easy · 10 min · practice plan
Count the permutations of three distinct values.
Passes when: #out should show six orders.
3.Copy the pathexercise · easy · 10 min · practice plan
Store a snapshot of the path and confirm it survives later changes.
Passes when: #out should show snapshot kept.
4.Prune a branchexercise · easy · 10 min · practice plan
Skip candidates over a limit and count what remains.
Passes when: #out should show two kept.
5.Combination sumexercise · medium · 10 min · practice plan
Count the ways to reach four using ones and twos, order ignored.
Passes when: #out should show three ways.
6.Fix the missing un-choosebug fix · easy · 8 min
Without popping, the path keeps growing and the counts are wrong.
Passes when: #out should show eight subsets.
7.Fix the path stored by referencebug fix · easy · 8 min · practice plan
Pushing the array itself stores something that keeps mutating.
Passes when: #out should show snapshot kept.
8.Fix the used marker never clearedbug fix · medium · 8 min · practice plan
Leaving values marked blocks every later branch.
Passes when: #out should show six orders.
9.Fix the recursion restarting from zerobug fix · easy · 8 min · practice plan
Recursing from the start allows the same value in a new order, so combinations repeat.
Passes when: #out should show three ways.
10.Fix the pruning that keeps everythingbug fix · easy · 8 min · practice plan
Skipping nothing explores branches that cannot lead anywhere.
Passes when: #out should show two kept.
Between them these tasks cover Choose and un-choose, Copying results and Pruning.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.