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 Recursive Call
What is the output of the following JavaScript code that solves the 0 1 Knapsack problem recursively for given weights and values?
DSA Javascript
function knapsack(W, weights, values, n) {
if (n === 0 || W === 0) return 0;
if (weights[n - 1] > W) {
return knapsack(W, weights, values, n - 1);
} else {
return Math.max(
values[n - 1] + knapsack(W - weights[n - 1], weights, values, n - 1),
knapsack(W, weights, values, n - 1)
);
}
}
const weights = [1, 3, 4, 5];
const values = [1, 4, 5, 7];
const W = 7;
const n = weights.length;
console.log(knapsack(W, weights, values, n));Attempts:
2 left
💡 Hint
Think about which items fit into the knapsack without exceeding the weight limit and maximize the value.
✗ Incorrect
The maximum value that fits into the knapsack of capacity 7 is 9, achieved by selecting items with weights 3 and 4 (values 4 and 5).
❓ Predict Output
intermediateOutput of 0 1 Knapsack Dynamic Programming Table
What is the maximum value stored in dp[4][7] after running the bottom-up dynamic programming solution for the 0 1 Knapsack problem with these inputs?
DSA Javascript
function knapsackDP(W, weights, values, n) {
const dp = Array.from({ length: n + 1 }, () => Array(W + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let w = 1; w <= W; w++) {
if (weights[i - 1] <= w) {
dp[i][w] = Math.max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
const weights = [1, 3, 4, 5];
const values = [1, 4, 5, 7];
const W = 7;
const n = weights.length;
console.log(knapsackDP(W, weights, values, n));Attempts:
2 left
💡 Hint
The dp table stores max values for subproblems. Check combinations that fit weight 7.
✗ Incorrect
The dp table correctly computes the max value as 9 for capacity 7 with given weights and values.
🧠 Conceptual
advancedTime Complexity of 0 1 Knapsack DP Solution
What is the time complexity of the bottom-up dynamic programming solution for the 0 1 Knapsack problem with n items and capacity W?
Attempts:
2 left
💡 Hint
Consider the nested loops over items and weight capacities.
✗ Incorrect
The solution uses two nested loops: one over items (n) and one over capacities (W), resulting in O(n * W) time complexity.
🔧 Debug
advancedIdentify the Bug in Recursive Knapsack Code
Which option contains the bug that causes incorrect output in this recursive 0 1 Knapsack implementation?
DSA Javascript
function knapsack(W, weights, values, n) {
if (n === 0 || W === 0) return 0;
if (weights[n - 1] >= W) {
return knapsack(W, weights, values, n - 1);
} else {
return Math.max(
values[n - 1] + knapsack(W - weights[n - 1], weights, values, n - 1),
knapsack(W, weights, values, n - 1)
);
}
}Attempts:
2 left
💡 Hint
Check the condition that decides if the current item fits in the knapsack.
✗ Incorrect
Using >= excludes items that exactly fit the remaining capacity, causing incorrect results.
🚀 Application
expertMaximum Value and Items Selected in 0 1 Knapsack
Given weights = [2, 3, 1, 4], values = [3, 4, 2, 5], and capacity W = 5, what is the maximum value and which items (by index starting at 0) are selected in the optimal 0 1 Knapsack solution?
DSA Javascript
function knapsackDP(W, weights, values, n) {
const dp = Array.from({ length: n + 1 }, () => Array(W + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let w = 1; w <= W; w++) {
if (weights[i - 1] <= w) {
dp[i][w] = Math.max(values[i - 1] + dp[i - 1][w - weights[i - 1]], dp[i - 1][w]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
let res = dp[n][W];
let w = W;
const selected = [];
for (let i = n; i > 0 && res > 0; i--) {
if (res !== dp[i - 1][w]) {
selected.push(i - 1);
res -= values[i - 1];
w -= weights[i - 1];
}
}
return { maxValue: dp[n][W], items: selected.reverse() };
}
const weights = [2, 3, 1, 4];
const values = [3, 4, 2, 5];
const W = 5;
const n = weights.length;
console.log(knapsackDP(W, weights, values, n));Attempts:
2 left
💡 Hint
Trace the dp table and backtrack to find selected items.
✗ Incorrect
The maximum value is 7 by selecting items at indices 0 and 1 (weights 2 and 3, values 3 and 4).
