Challenge - 5 Problems
Fractional Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Fractional Knapsack Calculation
What is the output of the following JavaScript code that calculates the maximum value for the fractional knapsack problem?
DSA Javascript
function fractionalKnapsack(capacity, items) {
items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight));
let totalValue = 0;
for (const item of items) {
if (capacity === 0) break;
let amount = Math.min(item.weight, capacity);
totalValue += amount * (item.value / item.weight);
capacity -= amount;
}
return totalValue.toFixed(2);
}
const items = [
{ weight: 10, value: 60 },
{ weight: 20, value: 100 },
{ weight: 30, value: 120 }
];
console.log(fractionalKnapsack(50, items));Attempts:
2 left
💡 Hint
Sort items by value per weight and take as much as possible from the highest ratio first.
✗ Incorrect
The function sorts items by value per weight descending. It takes full 10 weight of first item (value 60), full 20 weight of second (value 100), and 20 weight of third (partial, value 80). Total value = 60 + 100 + 80 = 240.
🧠 Conceptual
intermediateKey Idea Behind Fractional Knapsack
Which statement best describes the key idea behind the fractional knapsack problem?
Attempts:
2 left
💡 Hint
Fractional knapsack allows partial items, unlike 0/1 knapsack.
✗ Incorrect
The fractional knapsack problem allows taking any fraction of an item to maximize value within the capacity limit.
❓ Predict Output
advancedOutput with Different Capacity
What is the output of this code when the knapsack capacity is 40?
DSA Javascript
function fractionalKnapsack(capacity, items) {
items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight));
let totalValue = 0;
for (const item of items) {
if (capacity === 0) break;
let amount = Math.min(item.weight, capacity);
totalValue += amount * (item.value / item.weight);
capacity -= amount;
}
return totalValue.toFixed(2);
}
const items = [
{ weight: 10, value: 60 },
{ weight: 20, value: 100 },
{ weight: 30, value: 120 }
];
console.log(fractionalKnapsack(40, items));Attempts:
2 left
💡 Hint
Take full items with highest value/weight ratio until capacity is reached.
✗ Incorrect
Take full 10 weight of first item (60), full 20 weight of second (100), and 10 weight of third (40 value). Total = 60 + 100 + 40 = 200.
🔧 Debug
advancedIdentify the Error in Fractional Knapsack Code
What error will this code produce when run?
DSA Javascript
function fractionalKnapsack(capacity, items) {
items.sort((a, b) => (b.value / b.weight) - (a.value / a.weight));
let totalValue = 0;
for (const item of items) {
if (capacity === 0) break;
let amount = Math.min(item.weight, capacity);
totalValue += amount * (item.value / item.weight);
capacity -= amount
}
return totalValue.toFixed(2);
}
const items = [
{ weight: 10, value: 60 },
{ weight: 20, value: 100 },
{ weight: 30, value: 120 }
];
console.log(fractionalKnapsack(50, items));Attempts:
2 left
💡 Hint
JavaScript allows missing semicolons in many cases.
✗ Incorrect
JavaScript does not require semicolons at the end of statements; the code runs fine and outputs 240.00.
🧠 Conceptual
expertWhy Greedy Works for Fractional Knapsack but Not 0/1 Knapsack
Why does the greedy approach guarantee an optimal solution for the fractional knapsack problem but not for the 0/1 knapsack problem?
Attempts:
2 left
💡 Hint
Think about how partial items affect the solution space.
✗ Incorrect
Fractional knapsack allows splitting items, so taking the highest value/weight ratio first always leads to the best total value. 0/1 knapsack requires whole items, so greedy can miss better combinations.
