Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice
Fractional knapsack, capacity 16 (items can be split). Items (weight, value): (3, 8), (7, 6), (3, 8), (6, 8). Using the greedy value/weight-ratio algorithm, what is the maximum total value (rounded to 2 decimals)?
- A. 26.43
- B. 28.43
- C. 24
- D. 27.43
Correct answer: D. 27.43
Sorting items by value/weight ratio and greedily taking as much as possible of the best ratio first (splitting the last item to exactly fill the capacity) is optimal for the fractional version, giving 27.43. This greedy choice is only optimal because items can be split; the 0/1 version of the same items would instead give 24.
More Coding Interview Problems questions
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (5, 14), (6, 9), (2, 9), (5, 13). Using the greedy…
- Fractional knapsack, capacity 12 (items can be split). Items (weight, value): (3, 12), (6, 9), (3, 13), (5, 9), (5, 6). Using the greedy…
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- Using a min-heap of size k (push each element, pop when size exceeds k, so the heap always holds the k largest seen so far), what is the k…
- Fractional knapsack, capacity 17 (items can be split). Items (weight, value): (4, 14), (7, 5), (5, 4), (5, 10). Using the greedy…
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…