Data Structures & AlgorithmsHardAmazon · Microsoft OA

Problem: Balloons span horizontal intervals [start, end]; an arrow shot at x bursts every balloon with start ≤ x ≤ end. Find the minimum number of arrows.

Problem: Balloons span horizontal intervals [start, end]; an arrow shot at x bursts every balloon with start ≤ x ≤ end. Find the minimum number of arrows.
def arrows(b):
    b.sort(key=lambda p: p[1]); cnt, x = 0, float('-inf')
    for s, e in b:
        if s > x:
            cnt += 1; x = e
    return cnt
print(arrows([[1,4],[2,3],[3,8],[5,7],[9,10],[8,9]]))
What is printed?
  1. A. 3
  2. B. 2
  3. C. 4
  4. D. 5

Correct answer: A. 3

Sorted by end: [2,3], [1,4], [5,7], [3,8], [8,9], [9,10]. Arrow at 3 bursts [2,3], [1,4], [3,8]; [5,7] needs an arrow at 7; [8,9] needs one at 9, which also bursts [9,10] because endpoints count. Total 3.

More Coding Interview Problems questions

Exams that ask this topic