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

Balloons are closed horizontal intervals [10,16], [2,8], [1,6], [7,12]. One vertical arrow at x bursts every balloon whose interval contains x, endpoints included. What is the minimum number of arrows needed?

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

Correct answer: D. 2

Greedy: sort by right end and shoot at the earliest end; every balloon that starts at or before that point is burst too. The arrow at x = 6 bursts [1,6] and [2,8], and the other arrow at x = 12 bursts [7,12] and [10,16]; the answer is 2.

More Coding Interview Problems questions

Exams that ask this topic