Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
Seven people weigh [3, 5, 3, 4, 2, 6, 1] (kg). A boat carries at most two people and at most 7 kg in total. What is the minimum number of boats needed to carry everyone?
- A. 4
- B. 5
- C. 3
- D. 6
Correct answer: A. 4
Pair the heaviest remaining person with the lightest one whenever the pair fits, otherwise send the heaviest alone. Two pointers on the sorted list achieve this in linear time, and the greedy count equals the brute-force minimum.
More Coding Interview Problems questions
- Sorted list [1, 2, 4, 6, 7, 9, 11]. How many index pairs i < j satisfy vals[i] + vals[j] <= 10?
- Four tasks each have (time, value): (3,2), (2,5), (4,4), (5,3). Choose any subset of tasks with total time at most 9 to maximise total…
- In how many ways can a set of 6 distinct people be partitioned into groups so that every group has an even number of people (groups are…
- From the values [3, 5, 6, 7, 10], choose any non-empty subset and XOR its elements together. What is the largest value that can be obtained?
- Four groups are G1 = {1, 4}, G2 = {2, 9}, G3 = {3, 7} and G4 = {5, 6}. Choosing exactly one element from each group, how many choices sum…
- Using each of the values 1, 2, 5, 9 and 20 at most once, which is the smallest positive integer that CANNOT be written as the sum of some…