Challenge - 5 Problems
Unbounded Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Unbounded Knapsack DP Table
What is the final maximum value returned by the unbounded knapsack function for the given input?
DSA C++
int unboundedKnapsack(int W, int n, int val[], int wt[]) { int dp[W+1]; for (int i = 0; i <= W; i++) dp[i] = 0; for (int i = 0; i <= W; i++) { for (int j = 0; j < n; j++) { if (wt[j] <= i) { dp[i] = std::max(dp[i], dp[i - wt[j]] + val[j]); } } } return dp[W]; } int main() { int val[] = {10, 40, 50, 70}; int wt[] = {1, 3, 4, 5}; int W = 8; int n = 4; int result = unboundedKnapsack(W, n, val, wt); std::cout << result << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about how many times you can take each item to maximize value without exceeding weight.
✗ Incorrect
The maximum value for weight 8 is 120 by taking items with weights 3 and 5 (40 + 70) or multiple smaller items to maximize value.
🧠 Conceptual
intermediateUnderstanding Unbounded Knapsack Item Choices
In the unbounded knapsack problem, why can items be chosen multiple times compared to the 0/1 knapsack problem?
Attempts:
2 left
💡 Hint
Think about the difference in constraints between 0/1 knapsack and unbounded knapsack.
✗ Incorrect
Unbounded knapsack allows unlimited copies of each item, unlike 0/1 knapsack where each item can be chosen at most once.
🔧 Debug
advancedIdentify the Bug in Unbounded Knapsack Implementation
What error will this code produce when run, and why?
DSA C++
int unboundedKnapsack(int W, int n, int val[], int wt[]) { int dp[W+1]; for (int i = 0; i < W; i++) dp[i] = 0; for (int i = 0; i <= W; i++) { for (int j = 0; j < n; j++) { if (wt[j] <= i) { dp[i] = std::max(dp[i], dp[i - wt[j]] + val[j]); } } } return dp[W]; }
Attempts:
2 left
💡 Hint
Check the initialization loop for dp array carefully.
✗ Incorrect
The dp array is initialized only up to dp[W-1], leaving dp[W] uninitialized. This causes undefined behavior when dp[W] is accessed.
❓ Predict Output
advancedOutput of Recursive Unbounded Knapsack with Memoization
What is the output of the following code snippet?
DSA C++
#include <iostream> #include <vector> #include <algorithm> int unboundedKnapsackRec(int W, int n, int val[], int wt[], std::vector<int>& dp) { if (W == 0) return 0; if (dp[W] != -1) return dp[W]; int maxVal = 0; for (int i = 0; i < n; i++) { if (wt[i] <= W) { maxVal = std::max(maxVal, val[i] + unboundedKnapsackRec(W - wt[i], n, val, wt, dp)); } } dp[W] = maxVal; return maxVal; } int main() { int val[] = {5, 10, 15}; int wt[] = {1, 3, 4}; int W = 7; int n = 3; std::vector<int> dp(W+1, -1); std::cout << unboundedKnapsackRec(W, n, val, wt, dp) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Try to find the combination of items that maximizes value for weight 7.
✗ Incorrect
The best combination is two items of weight 3 (10 + 10 = 20) plus one item of weight 1 (5) for total 25, or one item of weight 4 (15) plus three items of weight 1 (5*3=15) total 30, or other combinations. The maximum is 35 by taking one item of weight 4 (15) plus one item of weight 3 (10) plus three items of weight 1 (5*3=15) total 40 is not possible because weight would exceed 7. The code returns 35 as max value.
🧠 Conceptual
expertTime Complexity of Unbounded Knapsack DP Solution
What is the time complexity of the standard dynamic programming solution for the unbounded knapsack problem with capacity W and n items?
Attempts:
2 left
💡 Hint
Consider the nested loops in the DP solution.
✗ Incorrect
The DP solution uses two nested loops: one over weights from 0 to W, and one over n items, resulting in O(n * W) time complexity.
