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 Calculation
What is the maximum value that can be carried in the knapsack with capacity 50 using the given weights and values?
DSA Rust
fn knapsack(values: &[i32], weights: &[i32], capacity: usize) -> i32 {
let n = values.len();
let mut dp = vec![vec![0; capacity + 1]; n + 1];
for i in 1..=n {
for w in 0..=capacity {
if weights[i - 1] as usize <= w {
dp[i][w] = dp[i - 1][w].max(dp[i - 1][w - weights[i - 1] as usize] + values[i - 1]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
dp[n][capacity]
}
fn main() {
let values = [60, 100, 120];
let weights = [10, 20, 30];
let capacity = 50;
println!("{}", knapsack(&values, &weights, capacity));
}Attempts:
2 left
💡 Hint
Think about which items fit within the capacity and maximize value.
✗ Incorrect
The knapsack can hold items with weights 20 and 30 for a total value of 100 + 120 = 220, which is the maximum possible.
❓ Predict Output
intermediateValue of dp Table Cell in 0 1 Knapsack
What is the value of dp[2][20] after processing the first two items in the knapsack problem with values [60, 100] and weights [10, 20] and capacity 50?
DSA Rust
fn knapsack_dp_cell() -> i32 {
let values = [60, 100];
let weights = [10, 20];
let capacity = 50;
let n = values.len();
let mut dp = vec![vec![0; capacity + 1]; n + 1];
for i in 1..=n {
for w in 0..=capacity {
if weights[i - 1] as usize <= w {
dp[i][w] = dp[i - 1][w].max(dp[i - 1][w - weights[i - 1] as usize] + values[i - 1]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
dp[2][20]
}
fn main() {
println!("{}", knapsack_dp_cell());
}Attempts:
2 left
💡 Hint
Check if the second item fits exactly in capacity 20.
✗ Incorrect
At capacity 20, the second item (weight 20, value 100) fits exactly, so dp[2][20] = max(dp[1][20], dp[1][0] + 100) = max(60, 100) = 100.
🔧 Debug
advancedIdentify the error in this 0 1 Knapsack implementation
What error will this Rust code produce when run?
DSA Rust
fn knapsack(values: &[i32], weights: &[i32], capacity: usize) -> i32 {
let n = values.len();
let mut dp = vec![vec![0; capacity]; n + 1];
for i in 1..=n {
for w in 0..capacity {
if weights[i - 1] as usize <= w {
dp[i][w] = dp[i - 1][w].max(dp[i - 1][w - weights[i - 1] as usize] + values[i - 1]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
dp[n][capacity]
}
fn main() {
let values = [60, 100, 120];
let weights = [10, 20, 30];
let capacity = 50;
println!("{}", knapsack(&values, &weights, capacity));
}Attempts:
2 left
💡 Hint
Check the range of the inner loop for w.
✗ Incorrect
The inner loop runs w from 0 to capacity-1, so dp[i][capacity] is never assigned and accessing dp[n][capacity] causes an index out of bounds panic.
🧠 Conceptual
advancedTime Complexity of 0 1 Knapsack DP Solution
What is the time complexity of the 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 capacity.
✗ Incorrect
The DP solution uses a table of size n by W and fills each cell once, so time complexity is O(n * W).
🚀 Application
expertMaximum Value and Items Selected in 0 1 Knapsack
Given values [20, 5, 10, 40, 15, 25], weights [1, 2, 3, 8, 7, 4], and capacity 10, which set of items gives the maximum value without exceeding capacity?
Attempts:
2 left
💡 Hint
Sum weights and values of each option and check capacity limit.
✗ Incorrect
Option A items have weights 1 + 2 + 4 = 7 <= 10 and values 20 + 5 + 25 = 50, which is the maximum value achievable within capacity.
