Challenge - 5 Problems
Unbounded Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Unbounded Knapsack DP Table
What is the final maximum value returned by this Go function for unbounded knapsack with given weights, values, and capacity?
DSA Go
package main import "fmt" func unboundedKnapsack(W int, wt []int, val []int) int { dp := make([]int, W+1) for i := 0; i <= W; i++ { for j := 0; j < len(wt); j++ { if wt[j] <= i { dp[i] = max(dp[i], dp[i-wt[j]]+val[j]) } } } return dp[W] } func max(a, b int) int { if a > b { return a } return b } func main() { weights := []int{2, 3, 4} values := []int{40, 50, 60} capacity := 8 fmt.Println(unboundedKnapsack(capacity, weights, values)) }
Attempts:
2 left
💡 Hint
Think about how many times you can pick items to maximize value without exceeding capacity.
✗ Incorrect
The function uses dynamic programming to find the maximum value for capacity 8. The best combination is four items of weight 2 (value 40 each) totaling 160.
🧠 Conceptual
intermediateKey Difference Between 0/1 and Unbounded Knapsack
Which statement correctly describes the main difference between 0/1 Knapsack and Unbounded Knapsack problems?
Attempts:
2 left
💡 Hint
Think about how many times you can pick the same item in each problem.
✗ Incorrect
Unbounded Knapsack allows multiple picks of the same item, unlike 0/1 Knapsack which allows only one pick per item.
🔧 Debug
advancedIdentify the Error in Unbounded Knapsack Implementation
What error will this Go code produce when running the unbounded knapsack function?
DSA Go
func max(a, b int) int { if a > b { return a } return b } func unboundedKnapsack(W int, wt []int, val []int) int { dp := make([]int, W) for i := 0; i <= W; i++ { for j := 0; j < len(wt); j++ { if wt[j] <= i { dp[i] = max(dp[i], dp[i-wt[j]]+val[j]) } } } return dp[W] }
Attempts:
2 left
💡 Hint
Check the size of dp array and the indices accessed.
✗ Incorrect
dp is created with size W, but code accesses dp[W], which is out of range causing panic.
🚀 Application
advancedMaximum Value for Given Items and Capacity
Given items with weights [1, 3, 4] and values [15, 50, 60], and capacity 7, what is the maximum value achievable using unbounded knapsack?
Attempts:
2 left
💡 Hint
Try combinations of items that fit into capacity 7 with repetition.
✗ Incorrect
The maximum value is 115, achieved by taking two items of weight 3 (value 50 each, total 100) and one item of weight 1 (value 15).
❓ Predict Output
expertOutput of Recursive Unbounded Knapsack with Memoization
What is the output of this Go program that uses recursion with memoization for unbounded knapsack?
DSA Go
package main import "fmt" func unboundedKnapsackRec(W int, wt []int, val []int, memo map[int]int) int { if W == 0 { return 0 } if v, ok := memo[W]; ok { return v } maxVal := 0 for i := 0; i < len(wt); i++ { if wt[i] <= W { valWithItem := val[i] + unboundedKnapsackRec(W-wt[i], wt, val, memo) if valWithItem > maxVal { maxVal = valWithItem } } } memo[W] = maxVal return maxVal } func main() { weights := []int{1, 3, 4} values := []int{15, 50, 60} capacity := 7 memo := make(map[int]int) fmt.Println(unboundedKnapsackRec(capacity, weights, values, memo)) }
Attempts:
2 left
💡 Hint
Memoization stores results for capacities to avoid repeated work.
✗ Incorrect
The recursive function finds max value 115 for capacity 7 with given items, same as iterative solution.
