Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
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 halves of the sorted list.
- A. 5
- B. 3
- C. 2
- D. 1
Correct answer: D. 1
Split the eight values into halves and enumerate all subsets of each half with their sizes. Matching a subset of size i from one half with a subset of size 4 - i from the other half gives every 4-element subset exactly once.
More Coding Interview Problems questions
- 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…
- Closed intervals [1,4], [2,6], [5,8], [7,9], [8,12], [10,11] must each contain at least one chosen integer point. What is the minimum…
- Jobs are triples (start, end, weight): (1,3,2), (2,5,4), (4,6,3), (6,8,5), (5,9,6). Jobs may be chosen together only when one ends no…
- Array a = [2, 1, 3, 4, 1, 2, 5, 1]. How many non-empty contiguous subarrays contain exactly as many odd numbers as even numbers?
- Array a = [4, -1, 2, -3, 5, -2, 1, 1, -4]. How many DISTINCT values appear among the sums of all non-empty contiguous subarrays?