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 Recursive Call
What is the output printed by the following Java code snippet that solves the unbounded knapsack problem recursively?
DSA Java
public class UnboundedKnapsack { static int unboundedKnapsack(int W, int[] wt, int[] val, int n) { if (n == 0 || W == 0) return 0; if (wt[n-1] <= W) { return Math.max(val[n-1] + unboundedKnapsack(W - wt[n-1], wt, val, n), unboundedKnapsack(W, wt, val, n-1)); } else { return unboundedKnapsack(W, wt, val, n-1); } } public static void main(String[] args) { int W = 8; int[] val = {10, 40, 50, 70}; int[] wt = {1, 3, 4, 5}; int n = val.length; System.out.println(unboundedKnapsack(W, wt, val, n)); } }
Attempts:
2 left
💡 Hint
Think about how many times you can pick items with weight 1 and 3 to maximize value.
✗ Incorrect
The maximum value for weight 8 is 110 by picking the item with weight 5 (value 70) once and the item with weight 3 (value 40) once.
❓ Predict Output
intermediateOutput of Bottom-Up DP Table for Unbounded Knapsack
What is the final maximum value printed by this bottom-up dynamic programming Java code for unbounded knapsack?
DSA Java
public class UnboundedKnapsackDP { public static int unboundedKnapsack(int W, int[] wt, int[] val) { int[] dp = new int[W + 1]; for (int i = 0; i <= W; i++) { for (int j = 0; j < wt.length; j++) { if (wt[j] <= i) { dp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j]); } } } return dp[W]; } public static void main(String[] args) { int W = 7; int[] val = {15, 14, 10}; int[] wt = {1, 5, 3}; System.out.println(unboundedKnapsack(W, wt, val)); } }
Attempts:
2 left
💡 Hint
Try to fill the knapsack with the item of weight 1 as many times as possible.
✗ Incorrect
The best value is 15 * 7 = 105 by taking the item with weight 1 seven times.
🔧 Debug
advancedIdentify the Runtime Error in Unbounded Knapsack Code
Which option will cause a runtime error when running this unbounded knapsack Java code?
DSA Java
public class UnboundedKnapsackBug { public static int unboundedKnapsack(int W, int[] wt, int[] val) { int[] dp = new int[W]; for (int i = 0; i <= W; i++) { for (int j = 0; j < wt.length; j++) { if (wt[j] <= i) { dp[i] = Math.max(dp[i], dp[i - wt[j]] + val[j]); } } } return dp[W]; } public static void main(String[] args) { int W = 5; int[] val = {10, 30, 20}; int[] wt = {1, 3, 2}; System.out.println(unboundedKnapsack(W, wt, val)); } }
Attempts:
2 left
💡 Hint
Check the size of the dp array and the indices used.
✗ Incorrect
The dp array is of size W (5), but code accesses dp[W] which is dp[5], causing ArrayIndexOutOfBoundsException.
🧠 Conceptual
advancedWhy Use Unbounded Knapsack Instead of 0/1 Knapsack?
Which statement best explains when to use the unbounded knapsack problem instead of the 0/1 knapsack problem?
Attempts:
2 left
💡 Hint
Think about how many times you can pick each item.
✗ Incorrect
Unbounded knapsack allows unlimited copies of each item, unlike 0/1 knapsack which allows only one copy per item.
🚀 Application
expertMaximum Value and Items Chosen in Unbounded Knapsack
Given weights [2, 3, 5], values [50, 70, 100], and capacity 8, what is the maximum value and which items (by weight) are chosen in the unbounded knapsack solution?
Attempts:
2 left
💡 Hint
Try combinations of items that sum to 8 weight and maximize value.
✗ Incorrect
Choosing four items of weight 2 (value 50 each) gives total weight 8 and value 200, which is maximum.
