Minimum Number of Arrows to Burst Balloons solution explained

Minimum Number of Arrows to Burst Balloons gives each balloon as a horizontal range [start, end] and asks for the fewest vertical arrows that burst them all, where an arrow fired at x bursts every balloon with start <= x <= end. Sort the balloons by their right edge, fire the first arrow at the first right edge, and fire a new arrow at a balloon's right edge only when its start lies beyond the last arrow. That greedy is optimal and runs in O(n log n) time for the sort plus one O(n) pass.

By Published 8 min read

The problem in plain terms

Balloons are taped to a wall. Each one covers a horizontal stretch from start to end, both included; their heights do not matter. An arrow shot straight up from position x pops every balloon whose stretch includes x, and it keeps going. Given the list of stretches, return the smallest number of arrows that pops every balloon.

Balloon sets made up for this page
BalloonsAnswerWhere to shoot
[6, 9], [1, 5], [4, 7], [8, 12], [2, 3]3At 3, 7 and 12
[1, 4], [2, 6], [5, 8], [7, 9]2At 4 and 8
[0, 2], [2, 4]1At 2: the edges touch
[1, 10], [2, 3], [4, 5]2At 3 and 5; the long balloon is hit by either
Balloon sets made up for this page. Scroll sideways on a small screen.

What an interviewer checks

This is a greedy question, so the code is short and the reasoning carries the weight. The interviewer wants to see which edge you sort by and why, how you handle balloons that only touch, and whether you can justify that the greedy choice is never worse than any other plan. A good follow-up answer links it to the activity selection problem: the number of arrows equals the largest set of balloons where no two overlap.

A slow but sure answer

Some best plan only shoots at right edges: take any arrow and slide it right until it reaches the nearest right edge among the balloons it hits; it still hits all of them. So try sets of right edges in increasing size and stop at the first set that pops everything. This is exponential in the worst case, which is why it is only a reference for testing, but it makes the claim about right edges concrete.

from itertools import combinations

def min_arrows_brute(points):
    spots = sorted({end for _, end in points})   # some best answer only shoots at right edges
    for count in range(1, len(spots) + 1):
        for chosen in combinations(spots, count):
            if all(any(s <= a <= e for a in chosen) for s, e in points):
                return count
    return 0
Smallest set of right-edge shots, by trying every set of each size. Exponential; for checking only.

Greedy by right edge

Look at the balloon that ends first. Some arrow must pop it, and that arrow can be no further right than its end. Placing it exactly at its end is the best choice, because any position further left can only miss balloons that start later, never gain one. Fire there, skip every balloon that arrow already pops, and repeat with the next balloon that ends first among those left. After sorting by right edge, "already popped" is just a check that the balloon starts at or before the last arrow.

def find_min_arrow_shots(points):
    arrows = 0
    arrow_x = float('-inf')
    for start, end in sorted(points, key=lambda p: p[1]):   # by right edge
        if start > arrow_x:               # the last arrow missed this balloon
            arrows += 1
            arrow_x = end                 # shoot as far right as this balloon allows
    return arrows
Sort by right edge, one pass. O(n log n) time, O(n) for the sorted copy.

For [6, 9], [1, 5], [4, 7], [8, 12], [2, 3], the order by right edge is [2, 3], [1, 5], [4, 7], [6, 9], [8, 12]. The first arrow goes at 3 and also pops [1, 5], which starts at 1. [4, 7] starts at 4, past 3, so the second arrow goes at 7 and also pops [6, 9]. [8, 12] starts past 7, so the third arrow goes at 12. Three arrows.

The comparison is strict: a new arrow is needed only when start > arrow_x. A balloon that starts exactly where the last arrow flew is already popped, because both ends of a balloon count. Using >= here is a common bug, and the [0, 2], [2, 4] case catches it.

Sorting by left edge instead

Many people sort by start out of habit. That also works, with one change: keep track of the group's reach, the smallest right edge among the balloons in the current group, because the shared arrow has to fly at or left of every one of their ends. When a balloon starts beyond the reach, the group is closed and a new one begins. Without the shrinking step, the case [1, 10], [2, 3], [4, 5] goes wrong: the long first balloon sets the reach to 10, and all three look poppable by one arrow when two are needed.

function findMinArrowShots(points) {
  if (points.length === 0) return 0;
  const sorted = points.slice().sort((a, b) => a[0] - b[0]);   // by left edge this time
  let arrows = 1;
  let reach = sorted[0][1];          // the current arrow must fly at or left of this x
  for (let i = 1; i < sorted.length; i++) {
    const [start, end] = sorted[i];
    if (start > reach) {             // no x is shared with the current group
      arrows++;
      reach = end;
    } else {
      reach = Math.min(reach, end);  // the group's common part gets narrower
    }
  }
  return arrows;
}
Sort by left edge and shrink the reach. O(n log n). Sorts a copy, so the caller's array is untouched.

The comparator a[0] - b[0] is safe in JavaScript because numbers are 64-bit floats and edges at the 32-bit limits subtract exactly. In C++ or another language with fixed-size integers, the same subtraction can overflow for values near the limits, so compare with < instead.

Why the greedy count is the minimum

Every time the right-edge greedy fires a new arrow, it does so for a balloon that starts after the previous arrow, which sits at the previous chosen balloon's end. So the balloons that triggered arrows pairwise do not overlap. No single arrow can pop two balloons that do not overlap, so any plan needs at least as many arrows as there are such balloons. The greedy uses exactly that many, so it is optimal. The same argument shows the answer equals the maximum number of pairwise non-overlapping balloons, which is the dual view the intervals pattern guide also uses.

Running time

n balloons
ApproachTimeExtra space
Try sets of right-edge shotsExponentialO(n)
Greedy by right edgeO(n log n)O(n) for the sorted copy, or O(1) sorting in place
Greedy by left edge with shrinking reachO(n log n)O(n), or O(1) in place
n balloons. Scroll sideways on a small screen.

Edge cases

  • One balloon: one arrow.
  • Balloons that touch at a single point, which share an arrow.
  • A zero-width balloon like [3, 3].
  • Every balloon nested in one long balloon.
  • Edges near the 32-bit integer limits, for comparator overflow.

Mistakes

  • Using >= instead of > when deciding to fire a new arrow, which treats touching balloons as separate.
  • Sorting by start and never shrinking the reach.
  • Reusing merge-intervals code: merging overlaps and counting groups is a different question and gives the wrong count when a long balloon bridges two short ones.
  • Overflowing a subtraction comparator in a fixed-width language.
  • Sorting the caller's array in place when the function is not supposed to change it, without saying so.

How to say it

Fictional example

Justifying the greedy
Candidate
Some arrow has to pop the balloon that ends first, and it can't be right of that end. Putting it exactly at the end is never worse, since moving it left can only lose balloons.
Candidate
So I sort by right edge, shoot at the first end, skip everything that starts at or before that point, and repeat. Touching counts, so I only fire again when a start is strictly greater.
Interviewer
How do you know it's minimal?
Candidate
The balloons that made me fire don't overlap each other, and no arrow can pop two non-overlapping balloons. So any plan needs at least that many. It's n log n for the sort.

Merging overlapping ranges is covered in Merge Intervals, and the intervals pattern guide puts the stabbing, merging and scheduling variants side by side. The Skyline Problem is a harder sweep over ranges. For a change of pattern, Minimum Falling Path Sum and Delete and Earn are short DP problems.

Rehearse it with Natively

Greedy questions reward a crisp proof, and saying one aloud takes practice. On a Mac or Windows computer, Natively can read the balloon problem from a screenshot while you practise and show an approach and code in its overlay, which you can check against the strict comparison and the sorting choice described here. When you explain the proof in a mock interview, the transcript it saves lets you see whether the exchange argument came across. More on the AI interview assistant.

Questions people ask

How do you solve Minimum Number of Arrows to Burst Balloons?

Sort balloons by right edge. Shoot the first arrow at the first right edge, then go through the rest and shoot a new arrow at a balloon's right edge only when its start is greater than the last arrow's position.

What is the time complexity of Minimum Number of Arrows to Burst Balloons?

O(n log n) for the sort plus O(n) for the pass.

Do touching balloons share an arrow?

Yes. Both ends of a balloon count, so balloons [0, 2] and [2, 4] are popped by one arrow at 2. That is why the check for a new arrow is start > arrow, not >=.

Why is the greedy optimal for the balloons problem?

The balloons that trigger new arrows do not overlap each other, and one arrow cannot pop two non-overlapping balloons, so no plan can use fewer arrows than the greedy does.

Where to go next

Practice with the same assistant you would use on a call: it listens, transcribes and suggests answers from your own resume and the job description.

Facts on this page were last checked on . Product details come from the Natively app itself; if something here is out of date, tell us at hello@heynatively.com.

Keep reading

Put it on your own machine.

A live-conversation assistant that runs locally. Your conversations stay on your machine.

macOS 12+ · Windows 10/11

© 2026 Natively AI Private Limited

Source-available under the Natively Personal Use Source License v1.0 · IP geolocation by DB-IP