Data Structures & AlgorithmsModerateTCS NQT · Accenture
Problem: The fractional knapsack has capacity 50 and items (value, weight): (60, 10), (100, 20), (120, 30). Items may be taken fractionally. What is the maximum value, rounded down to an integer?
- A. 220
- B. 160
- C. 240
- D. 280
Correct answer: C. 240
Sort by value/weight (6, 5, 4): take items 1 and 2 whole (160, weight 30) and 20/30 of item 3 (80), for 240. The 0/1 knapsack optimum is 220, taking only whole items greedily stops at 160, and 280 ignores the capacity.
More Coding Interview Problems questions
- Problem: Huffman coding is applied to the frequencies 5, 9, 12, 13, 16, 45 by repeatedly merging the two smallest values (the merged value…
- Problem: The union of the intervals [1, 5], [4, 8], [10, 12], [11, 15], [20, 22] covers some integer points. How many integers lie in the…
- Problem: Given the intervals [1,4], [2,6], [3,5], [5,8], [7,9], [8,10] (closed, integer endpoints), what is the largest number of…
- Problem: A functional graph has n = 9 nodes where node i points to f(i) given by the list [1, 2, 0, 4, 5, 3, 3, 6, 7] (node i has exactly…
- Problem: Matrix chain multiplication: the dimensions of three matrices are given by the array p = [10, 30, 5, 60], meaning A1 is 10×30, A2…
- Problem: Maximum profit with at most two buy-sell transactions (one stock, you must sell before buying again) for the daily prices [3, 3…