Data Structures & AlgorithmsVery hardProduct company OA · Amazon/Microsoft-style OA

From the values [3, 34, 4, 12, 5, 2, 19, 8], choose any subset (the empty subset allowed). What is the smallest possible value of |(sum of chosen values) - 33|?

  1. A. 1
  2. B. 0
  3. C. 3
  4. D. 54

Correct answer: B. 0

Split the eight values into two halves of four, list the 16 subset sums of each half, sort one list and two-pointer against the other. Meet in the middle finds the best achievable gap to the target by pairing the two half-lists; here the closest subset misses 33 by 0.

More Coding Interview Problems questions

Exams that ask this topic