Data Structures & AlgorithmsVery hardProduct Company SDE · Amazon/Microsoft-style OA
Fractional knapsack, capacity 18 (items can be split). Items (weight, value): (6, 11), (5, 10), (3, 15), (6, 5). Using the greedy value/weight-ratio algorithm, what is the maximum total value (rounded to 2 decimals)?
- A. 40.33
- B. 39.33
- C. 36
- D. 38.33
Correct answer: B. 39.33
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 39.33. This greedy choice is only optimal because items can be split; the 0/1 version of the same items would instead give 36.
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…
- 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…
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…
- What is the length of the longest common subsequence of "ADDDBEEE" and "DDCCCCDB"?
- 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…
- Problem: 8 servers are joined by cables one at a time. Count the connected groups at the end and how many cables were redundant (joined…