It means each item can either be taken whole (1) or left (0). You cannot take fractions of an item.
Click to reveal answer
beginner
What is the main goal of the 0 1 Knapsack Problem?
To maximize the total value of items put in the knapsack without exceeding its weight capacity.
Click to reveal answer
intermediate
Which technique is commonly used to solve the 0 1 Knapsack Problem efficiently?
Dynamic Programming is used to solve it efficiently by building solutions to smaller subproblems.
Click to reveal answer
intermediate
In the dynamic programming solution, what does the table represent?
The table stores the maximum value achievable with the first i items and a knapsack capacity j.
Click to reveal answer
intermediate
Why can't greedy algorithms solve the 0 1 Knapsack Problem optimally?
Because taking the item with the best value or best value-to-weight ratio first may not lead to the best overall solution.
Click to reveal answer
In the 0 1 Knapsack Problem, what does the '1' signify?
AYou can take only one unit of each item
BYou can take fractions of items
CYou can take unlimited quantities of items
DYou must take all items
✗ Incorrect
The '1' means each item can be taken at most once, so only one unit of each item is allowed.
Which method is best to solve the 0 1 Knapsack Problem for large inputs?
ABrute force checking all subsets
BDynamic Programming
CGreedy algorithm
DSorting items by weight
✗ Incorrect
Dynamic Programming efficiently solves the problem by storing results of subproblems.
What does the DP table cell dp[i][w] represent?
AMaximum value using first i items with capacity w
BWeight of the ith item
CNumber of items taken
DMinimum weight to reach value w
✗ Incorrect
dp[i][w] stores the best value achievable with first i items and capacity w.
If an item’s weight is more than the current capacity, what happens in DP?
AItem is included anyway
BValue is doubled
CCapacity is increased
DItem is skipped
✗ Incorrect
If the item is too heavy, it cannot be included, so we skip it.
Why is the 0 1 Knapsack Problem not solved optimally by greedy approach?
ABecause it only works for fractional knapsack
BBecause it requires sorting items
CBecause greedy ignores combinations of items
DBecause it uses recursion
✗ Incorrect
Greedy approach fails because it does not consider all combinations, which are needed for optimal 0 1 knapsack.
Explain how dynamic programming solves the 0 1 Knapsack Problem step-by-step.
Think about how you choose items one by one and remember best values for smaller capacities.
You got /5 concepts.
Describe why the greedy method works for fractional knapsack but not for 0 1 knapsack.
Consider what happens if you cannot break items into parts.
You got /4 concepts.
Practice
(1/5)
1. What does the 0 1 Knapsack Problem aim to maximize given a set of items with weights and values?
easy
A. The total value of items without exceeding the weight limit
B. The total weight of items without exceeding the value limit
C. The number of items selected regardless of weight
D. The minimum weight of items selected
Solution
Step 1: Understand the problem goal
The 0 1 Knapsack Problem tries to select items to maximize total value without exceeding the weight capacity.
Step 2: Identify the constraint
The total weight of selected items must not exceed the knapsack's weight limit.
Final Answer:
The total value of items without exceeding the weight limit -> Option A
Quick Check:
Maximize value with weight limit = B [OK]
Hint: Remember: maximize value, not weight or count [OK]
Common Mistakes:
Confusing weight limit with value limit
Thinking the problem maximizes number of items
Ignoring the weight constraint
2. Which of the following is the correct way to initialize the DP array for the 0 1 Knapsack Problem in JavaScript?
easy
A. const dp = new Array(n + 1).fill(new Array(W + 1).fill(0));
B. const dp = Array(n + 1).fill(Array(W + 1).fill(0));
C. const dp = Array.from({ length: n + 1 }, () => Array(W + 1).fill(0));
D. const dp = Array(n).fill(Array(W).fill(0));
Solution
Step 1: Understand DP array initialization
We need a 2D array where each row is a separate array to avoid reference issues.
Step 2: Choose correct initialization
Using Array.from with a function creates independent inner arrays, preventing shared references.
Final Answer:
const dp = Array.from({ length: n + 1 }, () => Array(W + 1).fill(0)); -> Option C
Quick Check:
Independent inner arrays = A [OK]
Hint: Use Array.from with function to avoid shared references [OK]
Common Mistakes:
Using fill with array directly causing shared references
Initializing with wrong dimensions
Forgetting to fill inner arrays
3. What is the output of the following code snippet for weights = [1, 3, 4], values = [15, 50, 60], and W = 5?
function knapsack(weights, values, W) {
const n = weights.length;
const dp = Array.from({ length: n + 1 }, () => Array(W + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let w = 1; w <= W; 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][W];
}
console.log(knapsack([1, 3, 4], [15, 50, 60], 5));
medium
A. 65
B. 80
C. 60
D. 75
Solution
Step 1: Trace DP table filling
Check items one by one and weights up to 5: - Item 1 (weight 1, value 15): dp[1][1..5] = 15 - Item 2 (weight 3, value 50): dp[2][3] = max(15, 0+50)=50, dp[2][4]=max(15,15+50)=65, dp[2][5]=max(15,15+50)=65 - Item 3 (weight 4, value 60): dp[3][4]=max(65,0+60)=65, dp[3][5]=max(65,15+60)=75
Step 2: Find final answer
The maximum value for weight 5 is dp[3][5] = 75.
Final Answer:
75 -> Option D
Quick Check:
Max value at W=5 = 75 [OK]
Hint: Trace DP table row by row for each item [OK]
Common Mistakes:
Adding values without checking weight limit
Confusing indices in dp array
Forgetting to compare including vs excluding item
4. Identify the bug in this 0 1 Knapsack implementation and select the fix:
function knapsack(weights, values, W) {
const n = weights.length;
const dp = Array.from({ length: n + 1 }, () => Array(W + 1).fill(0));
for (let i = 1; i <= n; i++) {
for (let w = 1; w <= W; w++) {
if (weights[i] <= w) {
dp[i][w] = Math.max(dp[i - 1][w], dp[i - 1][w - weights[i]] + values[i]);
} else {
dp[i][w] = dp[i - 1][w];
}
}
}
return dp[n][W];
}
medium
A. Change weights[i] and values[i] to weights[i - 1] and values[i - 1]
B. Change loop to start i from 0 instead of 1
C. Initialize dp with zeros using fill(1)
D. Change inner loop to run from w = 0 to W - 1
Solution
Step 1: Identify index error in array access
Arrays in JavaScript are zero-indexed, but loop starts at 1, so weights[i] accesses out-of-bound or wrong element.
Step 2: Correct index usage
Use weights[i - 1] and values[i - 1] to access correct item corresponding to dp row i.
Final Answer:
Change weights[i] and values[i] to weights[i - 1] and values[i - 1] -> Option A
Quick Check:
Fix index offset = D [OK]
Hint: Remember dp row i corresponds to item i-1 [OK]
Common Mistakes:
Using wrong indices causing undefined values
Changing loop start incorrectly
Incorrect dp initialization
5. Given items with weights = [2, 3, 5, 7], values = [10, 5, 15, 7], and knapsack capacity W = 10, which set of items maximizes value without exceeding weight? Select the correct maximum value. Note: You can only take each item once.
hard
A. 25
B. 30
C. 27
D. 22
Solution
Step 1: Consider all item combinations within weight limit 10
Best valid combination is items 1,2,3 with total weight 10 and value 30. But since weight is exactly 10, it's valid. Check if any other combination gives more value: Items 1,3: weight 7, value 25 Items 3,4: weight 12 (exceeds) So max value is 30.
Final Answer:
30 -> Option B
Quick Check:
Max value with W=10 = 30 [OK]
Hint: Try all combos within weight limit to find max value [OK]
Common Mistakes:
Ignoring exact weight limit equality
Adding values of items exceeding capacity
Missing best combination
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.