Data Structures & AlgorithmsVery hardTCS NQT · Infosys pattern
Fractional knapsack, capacity 15 (items can be split). Items (weight, value): (2, 10), (6, 14), (4, 13), (2, 13), (8, 15). Using the greedy value/weight-ratio algorithm, what is the maximum total value (rounded to 2 decimals)?
- A. 50.88
- B. 52.88
- C. 51.88
- D. 50
Correct answer: C. 51.88
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 51.88. This greedy choice is only optimal because items can be split; the 0/1 version of the same items would instead give 50.
More Coding Interview Problems questions
- 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 "ABCECBA" and "ECEAAADE"?
- 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…
- 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 "BECCBACE" and "AEDDEA"?
- Problem: Dijkstra's algorithm (array-based, pick the unvisited vertex with smallest tentative distance each round) computes shortest…