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 items and capacity?
DSA Go
package main import "fmt" func knapsack(values, weights []int, capacity int) int { n := len(values) dp := make([][]int, n+1) for i := range dp { dp[i] = make([]int, capacity+1) } for i := 1; i <= n; i++ { for w := 1; w <= capacity; w++ { if weights[i-1] <= w { val1 := values[i-1] + dp[i-1][w-weights[i-1]] val2 := dp[i-1][w] if val1 > val2 { dp[i][w] = val1 } else { dp[i][w] = val2 } } else { dp[i][w] = dp[i-1][w] } } } return dp[n][capacity] } func main() { values := []int{60, 100, 120} weights := []int{10, 20, 30} capacity := 50 fmt.Println(knapsack(values, weights, capacity)) }
Attempts:
2 left
💡 Hint
Think about which items fit best without exceeding capacity.
✗ Incorrect
The maximum value is 220 by choosing items with weights 20 and 30 (values 100 + 120).
🧠 Conceptual
intermediateUnderstanding 0 1 Knapsack Problem Constraints
Which statement correctly describes the 0 1 Knapsack problem?
Attempts:
2 left
💡 Hint
Remember the meaning of '0 1' in the problem name.
✗ Incorrect
In 0 1 Knapsack, each item is either taken or not taken once, no fractions or repeats.
🔧 Debug
advancedIdentify the Bug in Knapsack Implementation
What error does the following knapsack code produce when run?
DSA Go
func knapsack(values, weights []int, capacity int) int { n := len(values) dp := make([][]int, n) for i := range dp { dp[i] = make([]int, capacity+1) } for i := 1; i <= n; i++ { for w := 1; w <= capacity; w++ { if weights[i-1] <= w { val1 := values[i-1] + dp[i-1][w-weights[i-1]] val2 := dp[i-1][w] if val1 > val2 { dp[i][w] = val1 } else { dp[i][w] = val2 } } else { dp[i][w] = dp[i-1][w] } } } return dp[n-1][capacity] }
Attempts:
2 left
💡 Hint
Check how dp slice is created and accessed.
✗ Incorrect
The dp slice is created with length n but accessed up to index n, causing out of range error.
🚀 Application
advancedNumber of Items Selected in Optimal Knapsack Solution
Given the items and capacity below, how many items are selected in the optimal solution?
DSA Go
values := []int{20, 5, 10, 40, 15, 25} weights := []int{1, 2, 3, 8, 7, 4} capacity := 10
Attempts:
2 left
💡 Hint
Trace which items fit best without exceeding capacity.
✗ Incorrect
The optimal solution selects 4 items with total weight ≤ 10 and maximum value.
❓ Predict Output
expertOutput of Recursive Knapsack with Memoization
What is the output of the following Go code implementing recursive 0 1 Knapsack with memoization?
DSA Go
package main import "fmt" func knapsackRec(values, weights []int, capacity, n int, memo [][]int) int { if n == 0 || capacity == 0 { return 0 } if memo[n][capacity] != -1 { return memo[n][capacity] } if weights[n-1] > capacity { memo[n][capacity] = knapsackRec(values, weights, capacity, n-1, memo) } else { val1 := values[n-1] + knapsackRec(values, weights, capacity-weights[n-1], n-1, memo) val2 := knapsackRec(values, weights, capacity, n-1, memo) if val1 > val2 { memo[n][capacity] = val1 } else { memo[n][capacity] = val2 } } return memo[n][capacity] } func main() { values := []int{24, 18, 18, 10} weights := []int{24, 10, 10, 7} capacity := 25 n := len(values) memo := make([][]int, n+1) for i := range memo { memo[i] = make([]int, capacity+1) for j := range memo[i] { memo[i][j] = -1 } } fmt.Println(knapsackRec(values, weights, capacity, n, memo)) }
Attempts:
2 left
💡 Hint
Consider which items fit best without exceeding capacity using recursion.
✗ Incorrect
The maximum value is 28 by choosing items with weights 10 and 7 (values 18 + 10).
