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 Recursive Call
What is the output of the following C# code that solves the Unbounded Knapsack problem recursively for given weights and values?
DSA C#
using System; class Program { static int UnboundedKnapsack(int W, int[] wt, int[] val, int n) { if (n == 0 || W == 0) return 0; if (wt[n - 1] <= W) { return Math.Max(val[n - 1] + UnboundedKnapsack(W - wt[n - 1], wt, val, n), UnboundedKnapsack(W, wt, val, n - 1)); } else { return UnboundedKnapsack(W, wt, val, n - 1); } } static void Main() { int[] val = {10, 40, 50, 70}; int[] wt = {1, 3, 4, 5}; int W = 8; int n = val.Length; Console.WriteLine(UnboundedKnapsack(W, wt, val, n)); } }
Attempts:
2 left
💡 Hint
Think about how many times you can pick items given the weight limit and the option to reuse items unlimited times.
✗ Incorrect
The maximum value for weight 8 is 110 by choosing items with weights 3 and 5 or multiple smaller items. The recursive function explores all combinations allowing unlimited picks.
❓ Predict Output
intermediateOutput of Bottom-Up DP for Unbounded Knapsack
What is the output of this bottom-up dynamic programming solution for the Unbounded Knapsack problem?
DSA C#
using System; class Program { static int UnboundedKnapsack(int W, int[] wt, int[] val, int n) { int[] dp = new int[W + 1]; for (int i = 0; i <= W; i++) { dp[i] = 0; } for (int i = 0; i <= W; i++) { for (int j = 0; j < n; j++) { if (wt[j] <= i) { dp[i] = Math.Max(dp[i], dp[i - wt[j]] + val[j]); } } } return dp[W]; } static void Main() { int[] val = {10, 40, 50, 70}; int[] wt = {1, 3, 4, 5}; int W = 8; int n = val.Length; Console.WriteLine(UnboundedKnapsack(W, wt, val, n)); } }
Attempts:
2 left
💡 Hint
The dp array stores max values for all weights up to W, considering unlimited picks.
✗ Incorrect
The bottom-up DP approach calculates the maximum value for each weight from 0 to W. The final dp[W] is 110 for the given inputs.
🔧 Debug
advancedIdentify the Runtime Error in Unbounded Knapsack Code
Which option will cause a runtime error when running this Unbounded Knapsack code snippet?
DSA C#
int UnboundedKnapsack(int W, int[] wt, int[] val, int n) { int[] dp = new int[W + 1]; for (int i = 1; i <= W; i++) { dp[i] = 0; } for (int i = 0; i <= W; i++) { for (int j = 0; j < n; j++) { if (wt[j] <= i) { dp[i] = Math.Max(dp[i], dp[i - wt[j]] + val[j]); } } } return dp[W]; }
Attempts:
2 left
💡 Hint
Check array indices carefully and initialization.
✗ Incorrect
The code initializes dp array correctly and accesses indices within bounds. No runtime error occurs.
🧠 Conceptual
advancedUnderstanding Time Complexity of Unbounded Knapsack DP
What is the time complexity of the bottom-up dynamic programming solution for the Unbounded Knapsack problem with capacity W and n items?
Attempts:
2 left
💡 Hint
Consider the nested loops over weights and items.
✗ Incorrect
The outer loop runs W times and the inner loop runs n times, resulting in O(n * W) complexity.
🚀 Application
expertMaximum Value and Items Chosen in Unbounded Knapsack
Given the bottom-up DP solution for Unbounded Knapsack, which option correctly outputs the maximum value and the count of items chosen for capacity 8 with values {10, 40, 50, 70} and weights {1, 3, 4, 5}?
DSA C#
using System; using System.Collections.Generic; class Program { static (int maxValue, Dictionary<int,int> itemCount) UnboundedKnapsack(int W, int[] wt, int[] val, int n) { int[] dp = new int[W + 1]; int[] itemChoice = new int[W + 1]; for (int i = 0; i <= W; i++) { dp[i] = 0; itemChoice[i] = -1; } for (int i = 0; i <= W; i++) { for (int j = 0; j < n; j++) { if (wt[j] <= i && dp[i] < dp[i - wt[j]] + val[j]) { dp[i] = dp[i - wt[j]] + val[j]; itemChoice[i] = j; } } } Dictionary<int,int> count = new Dictionary<int,int>(); int w = W; while (w > 0 && itemChoice[w] != -1) { int idx = itemChoice[w]; if (!count.ContainsKey(idx)) count[idx] = 0; count[idx]++; w -= wt[idx]; } return (dp[W], count); } static void Main() { int[] val = {10, 40, 50, 70}; int[] wt = {1, 3, 4, 5}; int W = 8; int n = val.Length; var result = UnboundedKnapsack(W, wt, val, n); Console.WriteLine($"Max Value: {result.maxValue}"); foreach(var kvp in result.itemCount) { Console.WriteLine($"Item {kvp.Key} count: {kvp.Value}"); } } }
Attempts:
2 left
💡 Hint
Trace back the chosen items from the itemChoice array to count how many times each item is picked.
✗ Incorrect
The maximum value is 110. The chosen items are two of item 1 (weight 3, value 40) and one of item 3 (weight 5, value 70).
