Data Structures & AlgorithmsVery hardGoldman Sachs OA · LeetCode medium-hard
Fractional knapsack, capacity 14 (items can be split). Items (weight, value): (8, 3), (6, 4), (4, 7), (3, 10). Using the greedy value/weight-ratio algorithm, what is the maximum total value (rounded to 2 decimals)?
- A. 21.38
- B. 21
- C. 22.38
- D. 20.38
Correct answer: A. 21.38
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 21.38. This greedy choice is only optimal because items can be split; the 0/1 version of the same items would instead give 21.
More Coding Interview Problems questions
- Using the merge-sort inversion-count technique (count, during each merge step, how many left-half elements are greater than the current…
- What is the length of the longest common subsequence of "EBBDBAD" and "ACBCBECC"?
- 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…
- What is the length of the longest common subsequence of "BDCEADE" and "EEABCADA"?
- Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (2, 10), (6, 14), (4, 13), (2, 13), (8, 15). Using the…
- 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…