Data Structures & AlgorithmsHardProduct Company SDE · greedy OA
Problem: Trucks carry boxes: each truck type is (count, units per box), and at most 10 boxes fit on the truck. Maximise units per truck with these types: [(1, 3), (2, 2), (3, 1)]. Use greedy by units per box and print the best total.
Problem: Trucks carry boxes: each truck type is (count, units per box), and at most 10 boxes fit on the truck. Maximise units per truck with these types: [(1, 3), (2, 2), (3, 1)]. Use greedy by units per box and print the best total.
types = [(1, 3), (2, 2), (3, 1)]
limit = 10
types.sort(key=lambda t: -t[1])
best = 0
for cnt, units in types:
take = min(cnt, limit)
best += take * units
limit -= take
print(best)What is printed?- A. 6
- B. 30
- C. 3
- D. 10
Correct answer: D. 10
Greedy takes the 3-unit box, both 2-unit boxes and all three 1-unit boxes (6 boxes, within the limit of 10): 3 + 4 + 3 = 10. Taking only one box per type gives 6, ignoring the count and filling all 10 slots with 3-unit boxes gives 30, and writing best = instead of best += keeps only the last type (3).
More Coding Interview Problems questions
- Problem: How many contiguous subarrays of [1, 2, 3, 4, 5] have sum exactly 5? Run the code. python a = [1, 2, 3, 4, 5] print(sum(1 for i…
- Problem: What is the length of the longest zigzag subsequence (values alternate strictly up and down) of [1, 7, 4, 9, 2, 5]? Run the DP…
- Problem: Two piles of stones (values [1, 6, 11, 5]) are to be divided into two groups whose sums differ as little as possible. What is the…
- Problem: You have 2 eggs and a building with 10 floors. What is the minimum number of drops needed in the worst case to find the highest…
- Problem: How many distinct ways can the string 'leetcode' be segmented into words from the dictionary {'leet', 'code', 'leetcode', 'lee'…
- Problem: Count the connected components of the undirected graph on 6 vertices with edges (0,1), (1,2), (3,4), (5,5) using DFS. Run the…