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.

  1. A. 0
  2. B. 2
  3. C. 4
  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

Exams that ask this topic