Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard

Fractional knapsack, capacity 12 (items can be split). Items (weight, value): (3, 12), (6, 9), (3, 13), (5, 9), (5, 6). Using the greedy value/weight-ratio algorithm, what is the maximum total value (rounded to 2 decimals)?

  1. A. 34
  2. B. 35.5
  3. C. 36.5
  4. D. 34.5

Correct answer: B. 35.5

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 35.5. This greedy choice is only optimal because items can be split; the 0/1 version of the same items would instead give 34.

More Coding Interview Problems questions

Exams that ask this topic