0
0
DSA Cprogramming~20 mins

Fractional Knapsack Problem in DSA C - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Fractional Knapsack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Fractional Knapsack Calculation
What is the output of the following C code that calculates the maximum value for fractional knapsack given weights and values arrays?
DSA C
typedef struct {
    int value;
    int weight;
} Item;

float fractionalKnapsack(int W, Item items[], int n) {
    float totalValue = 0.0;
    int i, curWeight = 0;
    float ratio[n];

    for (i = 0; i < n; i++) {
        ratio[i] = (float)items[i].value / items[i].weight;
    }

    for (i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (ratio[i] < ratio[j]) {
                float temp = ratio[i]; ratio[i] = ratio[j]; ratio[j] = temp;
                Item tempItem = items[i]; items[i] = items[j]; items[j] = tempItem;
            }
        }
    }

    for (i = 0; i < n; i++) {
        if (curWeight + items[i].weight <= W) {
            curWeight += items[i].weight;
            totalValue += items[i].value;
        } else {
            int remain = W - curWeight;
            totalValue += ratio[i] * remain;
            break;
        }
    }
    return totalValue;
}

#include <stdio.h>
int main() {
    Item items[] = {{60, 10}, {100, 20}, {120, 30}};
    int W = 50;
    int n = sizeof(items) / sizeof(items[0]);
    float maxValue = fractionalKnapsack(W, items, n);
    printf("%.2f\n", maxValue);
    return 0;
}
A220.00
B240.00
C180.00
D200.00
Attempts:
2 left
💡 Hint
Sort items by value-to-weight ratio and pick items until the knapsack is full.
🧠 Conceptual
intermediate
1:30remaining
Understanding Fractional Knapsack Strategy
Why does the fractional knapsack problem use the value-to-weight ratio to decide which items to pick first?
ABecause it maximizes the value gained per unit weight, ensuring the knapsack is filled with the most valuable fractions first.
BBecause it minimizes the total weight of items, allowing more items to fit regardless of value.
CBecause it sorts items alphabetically by name to maintain order.
DBecause it picks items randomly to avoid bias.
Attempts:
2 left
💡 Hint
Think about how to get the most value for the limited capacity.
🔧 Debug
advanced
2:00remaining
Identify the Bug in Fractional Knapsack Sorting
What is the bug in the following sorting code snippet used in fractional knapsack implementation?
DSA C
for (i = 0; i < n - 1; i++) {
    for (int j = i + 1; j < n; j++) {
        if (ratio[i] > ratio[j]) {
            float temp = ratio[i]; ratio[i] = ratio[j]; ratio[j] = temp;
            Item tempItem = items[i]; items[i] = items[j]; items[j] = tempItem;
        }
    }
}
AThe swap of items is missing, only ratio array is swapped.
BThe loop indices are incorrect and cause out-of-bounds access.
CThe comparison should be ratio[i] < ratio[j] to sort in descending order of ratio.
DThe code uses bubble sort which is inefficient and should be replaced.
Attempts:
2 left
💡 Hint
Check if the sorting order matches the problem requirement.
Predict Output
advanced
1:30remaining
Output of Fractional Knapsack with Zero Capacity
What is the output of this C code when the knapsack capacity is zero?
DSA C
typedef struct {
    int value;
    int weight;
} Item;

float fractionalKnapsack(int W, Item items[], int n) {
    float totalValue = 0.0;
    int i, curWeight = 0;
    float ratio[n];

    for (i = 0; i < n; i++) {
        ratio[i] = (float)items[i].value / items[i].weight;
    }

    for (i = 0; i < n - 1; i++) {
        for (int j = i + 1; j < n; j++) {
            if (ratio[i] < ratio[j]) {
                float temp = ratio[i]; ratio[i] = ratio[j]; ratio[j] = temp;
                Item tempItem = items[i]; items[i] = items[j]; items[j] = tempItem;
            }
        }
    }

    for (i = 0; i < n; i++) {
        if (curWeight + items[i].weight <= W) {
            curWeight += items[i].weight;
            totalValue += items[i].value;
        } else {
            int remain = W - curWeight;
            totalValue += ratio[i] * remain;
            break;
        }
    }
    return totalValue;
}

#include <stdio.h>
int main() {
    Item items[] = {{60, 10}, {100, 20}, {120, 30}};
    int W = 0;
    int n = sizeof(items) / sizeof(items[0]);
    float maxValue = fractionalKnapsack(W, items, n);
    printf("%.2f\n", maxValue);
    return 0;
}
A120.00
B60.00
C100.00
D0.00
Attempts:
2 left
💡 Hint
If capacity is zero, no items can be added.
🧠 Conceptual
expert
2:30remaining
Why Fractional Knapsack is Greedy but 0/1 Knapsack is Not
Why does the fractional knapsack problem allow a greedy approach to find the optimal solution, but the 0/1 knapsack problem does not?
ABecause fractional knapsack allows splitting items, so picking the best ratio first always leads to optimal, while 0/1 knapsack requires considering combinations.
BBecause 0/1 knapsack sorts items by weight only, which is not optimal.
CBecause fractional knapsack uses dynamic programming internally, unlike 0/1 knapsack.
DBecause 0/1 knapsack has fewer items, so greedy is unnecessary.
Attempts:
2 left
💡 Hint
Think about the difference between splitting items and taking whole items only.