Report how many subsets four items have.
Acceptance criteria
- 1.#out should show sixteen subsets.
DSA Tutorial · Dynamic Programming
These 10 tasks go with the Bitmask Dynamic Programming 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: “When a subproblem is identified by "which of these n things have I used", the state is a subset — and a subset of at most about twenty items fits neatly in a single integer.” .
1.Count the subsetsexercise · easy · 10 min
Report how many subsets four items have.
Passes when: #out should show sixteen subsets.
2.Test membershipexercise · easy · 10 min · practice plan
Report whether bit two is set in the mask five.
Passes when: #out should show member.
3.Add to the setexercise · easy · 10 min · practice plan
Set bit one in the mask four and show the result.
Passes when: #out should show mask 6.
4.The full maskexercise · easy · 10 min · practice plan
Show the mask with all three bits set.
Passes when: #out should show mask 7.
5.Count set bitsexercise · medium · 10 min · practice plan
Count how many items the mask eleven contains.
Passes when: #out should show three items.
6.Fix the subset count using multiplicationbug fix · easy · 8 min
The number of subsets is two to the n, not n squared.
Passes when: #out should show thirty two subsets.
7.Fix the membership test off by onebug fix · easy · 8 min · practice plan
Bit two is 1 shifted twice, not the value two.
Passes when: #out should show member.
8.Fix the add using exclusive orbug fix · easy · 8 min · practice plan
XOR toggles a bit, so adding twice removes it again.
Passes when: #out should show mask 6.
9.Fix the full mask off by onebug fix · easy · 8 min · practice plan
All n bits set is two to the n minus one.
Passes when: #out should show mask 7.
10.Fix the state size for a large nbug fix · medium · 8 min · practice plan
Two to the thirty is a billion states, far past what fits.
Passes when: #out should show too large.
Between them these tasks cover Subsets as integers, Mask operations and State size.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.