Data Structures & AlgorithmsHardProduct company OA · Amazon/Microsoft-style OA
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 the group sums? A meet-in-the-middle search over half the values finds it.
- A. 0
- B. 2
- C. 4
- D. 6
Correct answer: A. 0
Enumerating subset sums of each half and pairing the sums to bring the total of one group close to half of the grand total finds the optimum in about 2^(n/2) work. The answer is the difference of the closest pair of half-sums to total/2.
More Coding Interview Problems questions
- 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?
- Array a = [6, 1, 8, 3, 9, 4, 2, 7, 5] has distinct values. How many ordered pairs (i, j) with i != j satisfy a[i] - a[j] = 4?