Report which approach costs less when both are correct.
Acceptance criteria
- 1.#out should show greedy.
DSA Tutorial · Greedy
These 10 tasks go with the Greedy vs 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: “Both build an answer from smaller pieces. Greedy commits to one choice and moves on. DP keeps every choice open and picks the best at the end. That single difference decides which one is correct.” .
1.Greedy is cheaperexercise · easy · 10 min
Report which approach costs less when both are correct.
Passes when: #out should show greedy.
2.Find the counter-exampleexercise · easy · 10 min · practice plan
Show the greedy coin count for ten from one, five and six.
Passes when: #out should show greedy used 5.
3.DP finds the better answerexercise · easy · 10 min · practice plan
Show the optimal coin count for ten from one, five and six.
Passes when: #out should show dp used 2.
4.Fractional knapsackexercise · easy · 10 min · practice plan
Report whether greedy is correct when items can be split.
Passes when: #out should show greedy works.
5.Whole items onlyexercise · medium · 10 min · practice plan
Report the approach needed when items cannot be split.
Passes when: #out should show needs dp.
6.Fix greedy used on 0/1 knapsackbug fix · easy · 8 min
Whole items cannot be split, so the greedy ratio choice is unsound.
Passes when: #out should show needs dp.
7.Fix the counter-example that does not failbug fix · medium · 8 min · practice plan
Eleven is made exactly by six plus five, so it proves nothing.
Passes when: #out should show greedy used 5.
8.Fix DP dismissed as always slowerbug fix · easy · 8 min · practice plan
When greedy is wrong, a correct slower answer beats a fast wrong one.
Passes when: #out should show correctness first.
9.Fix the cost comparisonbug fix · easy · 8 min · practice plan
Greedy is the cheaper option only when it is also correct.
Passes when: #out should show greedy.
10.Fix the DP table missing its basebug fix · easy · 8 min · practice plan
Without a zero base case nothing is reachable and the answer is infinite.
Passes when: #out should show dp used 2.
Between them these tasks cover Choosing an approach, Counter-examples and Cost trade-offs.
Looking for a different chapter? See every practice set, or switch to the interview theory questions.