Challenge - 5 Problems
Knapsack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of 0 1 Knapsack DP Table
What is the final maximum value stored in the DP table for the given weights and values after running the 0 1 Knapsack algorithm?
DSA C#
int[] weights = {1, 3, 4, 5}; int[] values = {1, 4, 5, 7}; int capacity = 7; int[,] dp = new int[weights.Length + 1, capacity + 1]; for (int i = 1; i <= weights.Length; i++) { for (int w = 1; w <= capacity; w++) { if (weights[i - 1] <= w) { dp[i, w] = Math.Max(dp[i - 1, w], dp[i - 1, w - weights[i - 1]] + values[i - 1]); } else { dp[i, w] = dp[i - 1, w]; } } } Console.WriteLine(dp[weights.Length, capacity]);
Attempts:
2 left
💡 Hint
Think about which items fit best without exceeding the capacity.
✗ Incorrect
The maximum value achievable with capacity 7 is 11 by choosing items with weights 3 and 5 (values 4 + 7 = 11). Other combinations like 3 and 4 give 9. The DP table confirms 11 as max.
🧠 Conceptual
intermediateUnderstanding 0 1 Knapsack Item Inclusion
In the 0 1 Knapsack problem, if the weight of the current item is more than the current capacity, what does the algorithm do?
Attempts:
2 left
💡 Hint
Remember, 0 1 Knapsack means you either take the whole item or leave it.
✗ Incorrect
If the item is too heavy for the current capacity, the algorithm cannot include it, so it keeps the previous best value without this item.
🔧 Debug
advancedIdentify the bug in this 0 1 Knapsack implementation
What error will this code produce when run?
DSA C#
int[] weights = {2, 3, 4}; int[] values = {3, 4, 5}; int capacity = 5; int[,] dp = new int[weights.Length, capacity + 1]; for (int i = 1; i <= weights.Length; i++) { for (int w = 1; w <= capacity; w++) { if (weights[i - 1] <= w) { dp[i, w] = Math.Max(dp[i - 1, w], dp[i - 1, w - weights[i - 1]] + values[i - 1]); } else { dp[i, w] = dp[i - 1, w]; } } } Console.WriteLine(dp[weights.Length - 1, capacity]);
Attempts:
2 left
💡 Hint
Check how the dp array is declared and accessed.
✗ Incorrect
The dp array is declared with size weights.Length but accessed with index i which goes up to weights.Length, causing an index out of range error.
❓ Predict Output
advancedOutput of Knapsack with Zero Capacity
What will be the output of the following code snippet?
DSA C#
int[] weights = {1, 2, 3}; int[] values = {10, 20, 30}; int capacity = 0; int[,] dp = new int[weights.Length + 1, capacity + 1]; for (int i = 1; i <= weights.Length; i++) { for (int w = 1; w <= capacity; w++) { if (weights[i - 1] <= w) { dp[i, w] = Math.Max(dp[i - 1, w], dp[i - 1, w - weights[i - 1]] + values[i - 1]); } else { dp[i, w] = dp[i - 1, w]; } } } Console.WriteLine(dp[weights.Length, capacity]);
Attempts:
2 left
💡 Hint
Think about what happens when capacity is zero.
✗ Incorrect
With zero capacity, no items can be included, so the maximum value is zero.
🧠 Conceptual
expertTime Complexity of 0 1 Knapsack DP Solution
What is the time complexity of the dynamic programming solution to the 0 1 Knapsack problem with n items and capacity W?
Attempts:
2 left
💡 Hint
Consider the nested loops over items and capacity.
✗ Incorrect
The DP solution uses a table of size n by W and fills it once, resulting in O(n * W) time complexity.
