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 Value Calculation
What is the output of the following Python code that calculates the maximum value for the fractional knapsack problem?
DSA Python
def fractional_knapsack(values, weights, capacity): index = list(range(len(values))) ratio = [v/w for v, w in zip(values, weights)] index.sort(key=lambda i: ratio[i], reverse=True) max_value = 0 for i in index: if weights[i] <= capacity: max_value += values[i] capacity -= weights[i] else: max_value += ratio[i] * capacity break return round(max_value, 2) values = [60, 100, 120] weights = [10, 20, 30] capacity = 50 print(fractional_knapsack(values, weights, capacity))
Attempts:
2 left
💡 Hint
Think about sorting items by value-to-weight ratio and taking fractions if needed.
✗ Incorrect
The function sorts items by value/weight ratio: 60/10=6, 100/20=5, 120/30=4. Sorted order is item with ratio 6, then 5, then 4. It takes full items 1 and 2, then fraction of item 3 to fill capacity 50. Total value is 60 + 100 + (4 * 20) = 240.
🧠 Conceptual
intermediateKey Idea Behind Fractional Knapsack
Which of the following best describes the main idea behind the fractional knapsack problem?
Attempts:
2 left
💡 Hint
Think about how to maximize value per unit weight.
✗ Incorrect
The fractional knapsack problem is solved by taking items with the highest value-to-weight ratio first, allowing fractions of items to fill the knapsack optimally.
🔧 Debug
advancedIdentify the Error in Fractional Knapsack Implementation
What error will the following code produce when executed?
DSA Python
def fractional_knapsack(values, weights, capacity): ratio = [v/w for v, w in zip(values, weights)] index = sorted(range(len(values)), key=lambda i: ratio[i]) max_value = 0 for i in index: if weights[i] <= capacity: max_value += values[i] capacity -= weights[i] else: max_value += ratio[i] * capacity break return max_value values = [60, 100, 120] weights = [10, 20, 30] capacity = 50 print(fractional_knapsack(values, weights, capacity))
Attempts:
2 left
💡 Hint
Check the sorting order of the ratio list.
✗ Incorrect
The code sorts the items by ratio in ascending order, so it takes the lowest value-to-weight ratio items first, which is incorrect for maximizing value. It results in a total value of 220.
❓ Predict Output
advancedOutput of Fractional Knapsack with Zero Capacity
What is the output of this code when the knapsack capacity is zero?
DSA Python
def fractional_knapsack(values, weights, capacity): ratio = [v/w for v, w in zip(values, weights)] index = sorted(range(len(values)), key=lambda i: ratio[i], reverse=True) max_value = 0 for i in index: if weights[i] <= capacity: max_value += values[i] capacity -= weights[i] else: max_value += ratio[i] * capacity break return max_value values = [60, 100, 120] weights = [10, 20, 30] capacity = 0 print(fractional_knapsack(values, weights, capacity))
Attempts:
2 left
💡 Hint
If capacity is zero, no items can be taken.
✗ Incorrect
Since the capacity is zero, the knapsack cannot hold any items, so the total value is zero.
🚀 Application
expertMaximum Value with Fractional Knapsack for Large Input
Given 4 items with values [500, 400, 300, 450] and weights [50, 40, 30, 45], and a knapsack capacity of 100, what is the maximum value the knapsack can hold?
Attempts:
2 left
💡 Hint
Calculate value-to-weight ratios and take items in descending order, possibly fractions.
✗ Incorrect
All items have the same value-to-weight ratio of 10, so the maximum value is capacity * 10 = 1000.0.
