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 DP table (2D list) after running the 0 1 Knapsack algorithm on the given input?
DSA Python
def knapsack(weights, values, capacity): n = len(weights) dp = [[0] * (capacity + 1) for _ in range(n + 1)] for i in range(1, n + 1): for w in range(capacity + 1): if weights[i-1] <= w: dp[i][w] = max(dp[i-1][w], dp[i-1][w - weights[i-1]] + values[i-1]) else: dp[i][w] = dp[i-1][w] return dp weights = [1, 3, 4] values = [15, 50, 60] capacity = 5 result = knapsack(weights, values, capacity) print(result)
Attempts:
2 left
💡 Hint
Trace the DP table row by row, considering if the current item fits or not.
✗ Incorrect
The DP table stores max values for each item and capacity. For item 3 (weight 4, value 60), at capacity 5, max is max of excluding or including it. Including it adds 60 + dp of capacity 1 from previous row (15), total 75.
🧠 Conceptual
intermediateMaximum Value in 0 1 Knapsack
Given weights = [2, 3, 5], values = [20, 30, 50], and capacity = 5, what is the maximum value achievable?
Attempts:
2 left
💡 Hint
Check which items fit without exceeding capacity and sum their values.
✗ Incorrect
Only the item with weight 5 and value 50 fits exactly. Other combinations exceed capacity or have less value.
🔧 Debug
advancedIdentify the error in this 0 1 Knapsack code snippet
What error does this code produce when run?
```python
def knapsack(weights, values, capacity):
n = len(weights)
dp = [[0] * (capacity + 1) for _ in range(n)]
for i in range(n):
for w in range(capacity + 1):
if weights[i] <= w:
dp[i][w] = max(dp[i-1][w], dp[i-1][w - weights[i]] + values[i])
else:
dp[i][w] = dp[i-1][w]
return dp[n-1][capacity]
weights = [1, 2]
values = [10, 20]
capacity = 3
print(knapsack(weights, values, capacity))
```
Attempts:
2 left
💡 Hint
Check how dp is initialized and accessed at dp[i-1].
✗ Incorrect
dp is initialized with n rows, but dp[i-1] for i=0 accesses dp[-1], which is valid in Python but logically incorrect here. Also, dp[0][w] tries to access dp[-1][w], which is out of intended bounds causing wrong results or IndexError in some environments.
🚀 Application
advancedFind selected items in 0 1 Knapsack
After computing the DP table for weights = [1, 3, 4], values = [15, 50, 60], capacity = 5, which items are selected to achieve max value?
Attempts:
2 left
💡 Hint
Trace back from dp[n][capacity] to find which items were included.
✗ Incorrect
Max value 75 comes from including item with weight 4 (value 60) and item with weight 1 (value 15).
🧠 Conceptual
expertTime Complexity of 0 1 Knapsack DP
What is the time complexity of the classic 0 1 Knapsack dynamic programming solution 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 each cell once, so time complexity is O(n * W).
