Data Structures & AlgorithmsModerateProduct company OA · Amazon/Microsoft-style OA

From the values [12, 3, 9, 15, 6, 20, 10, 7], choose 3 of them. What is the smallest possible difference between the largest and the smallest of the three chosen values?

  1. A. 5
  2. B. 3
  3. C. 2
  4. D. 17

Correct answer: B. 3

After sorting, the best three values form a contiguous block, so the answer is the minimum of a[i+2] - a[i] over all i. Choosing non-adjacent values can only widen the span.

More Coding Interview Problems questions

Exams that ask this topic