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

Values [3, 9, 4, 15, 8, 2, 11, 6] are given. Remove the fewest values so that the maximum minus the minimum of the remaining values is at most 5. What is the largest number of values that can remain?

  1. A. 3
  2. B. 6
  3. C. 7
  4. D. 4

Correct answer: D. 4

Sort the values and use a window over the sorted array in which max minus min is at most 5; the largest window is the most you can keep. Allowing a window width of 6 or sorting nothing gives a different count.

More Coding Interview Problems questions

Exams that ask this topic