Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (7, 7), (5, 7), (3, 14), (5, 11), (6, 5). Using the greedy value/weight-ratio algorithm, what is the maximum total value (rounded to 2 decimals)?
- A. 32
- B. 33
- C. 34
- D. 35
Correct answer: C. 34
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 34. This greedy choice is only optimal because items can be split; the 0/1 version of the same items would instead give 32.
More Coding Interview Problems questions
- What is the length of the longest common subsequence of "BEADCBBDA" and "BAAEDC"?
- A divide-and-conquer routine satisfies T(n) = 8T(n/2) + n². What is its running time (by the master theorem, extended for polylog terms…
- 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 "CBEDDDE" and "BEDECCBB"?
- Fractional knapsack, capacity 13 (items can be split). Items (weight, value): (8, 8), (3, 4), (6, 13), (8, 10), (6, 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…