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

Sprinklers at positions p with radius r cover the closed interval [p - r, p + r] of a garden. The sprinklers are (1,2), (4,1), (7,3), (12,2), (15,3), (9,2), and the garden is [0, 18]. What is the minimum number of sprinklers that must be switched on so that the whole garden is covered?

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

Correct answer: D. 5

Clip each sprinkler's coverage to the garden and sweep from left to right, always choosing among the sprinklers that start no later than the covered point the one reaching furthest. Counting sprinklers that reach the garden's edges misses the middle gaps.

More Coding Interview Problems questions

Exams that ask this topic