Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA
How many subsets of {1, 2, ..., 12} have a sum of exactly 20?
- A. 48
- B. 43
- C. 51
- D. 45
Correct answer: D. 45
Split {1..12} into {1..6} and {7..12}, enumerate the 64 subset sums of each half, and count matching pairs with a counter. The counts are over subsets, so different subsets with the same sum are all counted.
More Coding Interview Problems questions
- How many integers x with 1 <= x <= 200 have an odd number of 1-bits in their binary representation?
- Meetings (closed-open as half-open intervals) are [1,4), [1,4), [2,6), [5,8), [3,9), [7,10). A room can be reused the moment a meeting…
- Closed integer intervals [1,3], [2,5], [4,7], [6,9], [8,10], [5,6] are available. What is the minimum number needed so that every integer…
- Array [3, 1, 2, 5, 0, 4, 2]. How many contiguous subarrays have a sum equal to twice their length?
- Array [1, 3, 5, 4, 2, 6, 8, 7, 9]. What is the length of the shortest contiguous segment that, once sorted, makes the whole array sorted?
- How many permutations of (0, 1, 2, 3) have exactly one position i with p[i] = i?