Challenge - 5 Problems
Knapsack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output 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 Typescript
const weights = [1, 3, 4, 5]; const values = [1, 4, 5, 7]; const capacity = 7; function knapsack01(weights: number[], values: number[], capacity: number): number { const n = weights.length; const dp: number[][] = Array.from({ length: n + 1 }, () => Array(capacity + 1).fill(0)); for (let i = 1; i <= n; i++) { for (let 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]; } } } return dp[n][capacity]; } console.log(knapsack01(weights, values, capacity));
Attempts:
2 left
💡 Hint
Think about which items fit best without exceeding capacity 7.
✗ Incorrect
The DP table computes the maximum value of 9 by selecting the items with weights 3 and 4 (values 4 + 5 = 9). Combinations like weights 3+5=8 (values 4+7=11) exceed capacity, and 1+5=6 (1+7=8) is less.
❓ Predict Output
intermediate2:00remaining
Output of Knapsack Recursive Call Count
How many recursive calls are made when running the naive recursive 0 1 Knapsack function with weights [2, 3], values [4, 5], and capacity 5?
DSA Typescript
let callCount = 0; function knapsackRecursive(weights: number[], values: number[], capacity: number, n: number): number { callCount++; if (n === 0 || capacity === 0) return 0; if (weights[n - 1] > capacity) { return knapsackRecursive(weights, values, capacity, n - 1); } else { return Math.max( values[n - 1] + knapsackRecursive(weights, values, capacity - weights[n - 1], n - 1), knapsackRecursive(weights, values, capacity, n - 1) ); } } knapsackRecursive([2, 3], [4, 5], 5, 2); console.log(callCount);
Attempts:
2 left
💡 Hint
Count each function call including base cases.
✗ Incorrect
The recursive calls expand as follows: knapsackRecursive(5,2) calls knapsackRecursive(5,1) and knapsackRecursive(3,1), each further calls base cases. Total calls counted are 7.
🔧 Debug
advanced3:00remaining
Identify the bug in 0 1 Knapsack DP implementation
What is the output of the following code snippet and what is the bug causing it?
DSA Typescript
const weights = [1, 2, 3]; const values = [10, 15, 40]; const capacity = 5; function knapsack(weights: number[], values: number[], capacity: number): number { const n = weights.length; const dp: number[][] = Array.from({ length: n + 1 }, () => Array(capacity + 1).fill(0)); for (let i = 1; i <= n; i++) { for (let 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]; } } } return dp[n][capacity]; } console.log(knapsack(weights, values, capacity));
Attempts:
2 left
💡 Hint
Check array indexing inside the loops carefully.
✗ Incorrect
The code outputs 55 (correct value by coincidence). The bug is using weights[i] and values[i] instead of weights[i-1] and values[i-1], causing out-of-bounds access at i=n (undefined values). The if condition evaluates to false, copying the previous row without error here.
❓ Predict Output
advanced2:00remaining
Output of Knapsack with Memoization
What is the output of the following memoized 0 1 Knapsack function call?
DSA Typescript
const weights = [1, 3, 4, 5]; const values = [1, 4, 5, 7]; const capacity = 7; const memo: number[][] = Array.from({ length: weights.length + 1 }, () => Array(capacity + 1).fill(-1)); function knapsackMemo(weights: number[], values: number[], capacity: number, n: number): number { if (n === 0 || capacity === 0) return 0; if (memo[n][capacity] !== -1) return memo[n][capacity]; if (weights[n - 1] <= capacity) { memo[n][capacity] = Math.max( values[n - 1] + knapsackMemo(weights, values, capacity - weights[n - 1], n - 1), knapsackMemo(weights, values, capacity, n - 1) ); } else { memo[n][capacity] = knapsackMemo(weights, values, capacity, n - 1); } return memo[n][capacity]; } console.log(knapsackMemo(weights, values, capacity, weights.length));
Attempts:
2 left
💡 Hint
Memoization avoids repeated calculations but result matches DP solution.
✗ Incorrect
The memoized solution returns the same maximum value as the DP table approach, which is 9 for the given input.
🧠 Conceptual
expert2:00remaining
Time Complexity of 0 1 Knapsack Variants
Which statement correctly describes the time complexity of the 0 1 Knapsack problem algorithms?
Attempts:
2 left
💡 Hint
Consider how many subproblems DP solves compared to recursion.
✗ Incorrect
Naive recursion explores all subsets leading to exponential time. DP and memoization store results of subproblems, reducing complexity to O(n*W).