Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA
Values [3, 9, 4, 15, 8, 2, 11, 6] are given. Remove the fewest values so that the maximum minus the minimum of the remaining values is at most 5. What is the largest number of values that can remain?
- A. 3
- B. 6
- C. 7
- D. 4
Correct answer: D. 4
Sort the values and use a window over the sorted array in which max minus min is at most 5; the largest window is the most you can keep. Allowing a window width of 6 or sorting nothing gives a different count.
More Coding Interview Problems questions
- Sorted array a = [1, 2, 4, 7, 11, 16, 22, 29]. How many index pairs i < j have a[i] + a[j] <= 20?
- How many permutations p of {0, 1, 2, 3, 4, 5} satisfy p[i] != i for every position i? Use a bitmask DP over the set of values already…
- Can the numbers 1 through 20 be split into two groups with equal sums? Answer 1 if yes and 0 if no, computed with a subset-sum DP over bits.
- From the eight distinct values [4, 9, 11, 2, 7, 13, 5, 8], how many 4-element subsets sum to exactly 20? Use meet in the middle on the two…
- The eight values [3, 8, 1, 9, 4, 6, 7, 2] are divided into two groups of any sizes. What is the minimum possible absolute difference of…
- Busy intervals [2,5), [4,7), [9,12), [11,13), [15,16) lie inside the working day [0, 20). How many time units in the day are free, i.e…