Software Engineer (SDE)dsaadvanced3 interactive MCQs in app
Knapsack Variants
backward loop for 0/1 vs forward loop for unboundedpartition equal subset sum reductionO(n * W) pseudo-polynomial time
Core Concept
0/1 Knapsack chooses at most ONE copy of each item `(w, v)` up to capacity `W` (`dp[i][c] = max(dp[i-1][c], v + dp[i-1][c - w])`). When compressing to 1D `dp[c]`, **iteration direction determines semantics**: `0/1 Knapsack` MUST loop capacity `c` **BACKWARD (`W` down to `w`)** so an item isn’t reused in the same round, whereas **Unbounded Knapsack** loops **FORWARD (`w` up to `W`)** to permit reuse!
Worked Example
items `(w=2, v=3), (w=3, v=4)` with `W=4`: backward 1D `[0,0,3,3,3]` -> `[0,0,3,4,4]` -> max `4`.
Common Pitfall
looping capacity forward on 0/1 Subset Sum (`nums = [3], target = 6` falsely marks `dp[6] = True`!).
Recommended Readings & Practice
Practice Knapsack Variants 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.