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

Sorted values [1, 4, 6, 9, 13, 14, 18, 22] must be split into the fewest contiguous groups so that in every group the largest value minus the smallest is at most 5. How many groups are needed?

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

Correct answer: B. 3

Greedy with two pointers: start a group at the smallest unassigned value and extend it while the spread stays within 5. Because the array is sorted, the greedy group is always as large as possible, so no split can use fewer groups.

More Coding Interview Problems questions

Exams that ask this topic