Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
From the values [4, 9, 1, 12, 7, 3, 10, 6], keep as many as possible so that in the kept set the maximum is at most twice the minimum. How many values can be kept?
- A. 6
- B. 4
- C. 7
- D. 5
Correct answer: D. 5
After sorting, the kept set is a contiguous window whose right end is at most twice its left end. Two pointers find the widest such window in linear time, and the answer is its size.
More Coding Interview Problems questions
- 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…
- 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…