Find the fewest vertical arrows that burst every balloon interval

Quick Overview

Given balloons as closed intervals on the x-axis, find the minimum number of vertical arrows needed to burst all of them. The problem tests greedy interval reasoning, sorting by right endpoint, and an exchange argument for why the greedy choice is optimal.

Find the fewest vertical arrows that burst every balloon interval

Company: Zscaler

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Some spherical balloons are taped to a flat wall that represents the XY-plane. Each balloon is given as `points[i] = [x_start, x_end]`, the horizontal diameter of the balloon. You do not know the balloons' y-coordinates. Arrows can be shot straight up (in the positive y direction) from any point along the x-axis. An arrow keeps traveling up forever and bursts every balloon in its path: a balloon `[x_start, x_end]` is burst by an arrow shot at `x` when `x_start <= x <= x_end`. There is no limit on the number of arrows. Given `points`, return the minimum number of arrows that must be shot to burst all the balloons. ### Function Signature ```python def find_min_arrow_shots(points: list[list[int]]) -> int: ``` ### Rules - Each element of `points` is a two-element list `[x_start, x_end]`. - Both endpoints count. An arrow at exactly `x_start` or exactly `x_end` bursts the balloon. - The balloons can be given in any order. ### Constraints - `1 <= len(points) <= 10^5` - `len(points[i]) == 2` - `-2^31 <= x_start < x_end <= 2^31 - 1` ### Examples ```text Input: points = [[10,16],[2,8],[1,6],[7,12]] Output: 2 ``` An arrow at `x = 6` bursts `[2,8]` and `[1,6]`, and an arrow at `x = 11` bursts `[10,16]` and `[7,12]`. One arrow is not enough because `[1,6]` and `[7,12]` share no point. ```text Input: points = [[1,2],[3,4],[5,6],[7,8]] Output: 4 ``` No two balloons overlap, so each balloon needs its own arrow. ```text Input: points = [[1,2],[2,3],[3,4],[4,5]] Output: 2 ``` An arrow at `x = 2` bursts `[1,2]` and `[2,3]` because endpoints count, and an arrow at `x = 4` bursts `[3,4]` and `[4,5]`. One arrow cannot work since `[1,2]` and `[3,4]` do not touch.

Overview: Given balloons as closed intervals on the x-axis, find the minimum number of vertical arrows needed to burst all of them. The problem tests greedy interval reasoning, sorting by right endpoint, and an exchange argument for why the greedy choice is optimal.

Zscaler logo
Zscaler
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Some spherical balloons are taped to a flat wall that represents the XY-plane. Each balloon is given as points[i] = [x_start, x_end], the horizontal diameter of the balloon. You do not know the balloons' y-coordinates.

Arrows can be shot straight up (in the positive y direction) from any point along the x-axis. An arrow keeps traveling up forever and bursts every balloon in its path: a balloon [x_start, x_end] is burst by an arrow shot at x when x_start <= x <= x_end. There is no limit on the number of arrows.

Given points, return the minimum number of arrows that must be shot to burst all the balloons.

Function Signature

def find_min_arrow_shots(points: list[list[int]]) -> int:

Rules

  • Each element of points is a two-element list [x_start, x_end] .
  • Both endpoints count. An arrow at exactly x_start or exactly x_end bursts the balloon.
  • The balloons can be given in any order.

Constraints

  • 1 <= len(points) <= 10^5
  • len(points[i]) == 2
  • -2^31 <= x_start < x_end <= 2^31 - 1

Examples

Input: points = [[10,16],[2,8],[1,6],[7,12]]
Output: 2

An arrow at x = 6 bursts [2,8] and [1,6], and an arrow at x = 11 bursts [10,16] and [7,12]. One arrow is not enough because [1,6] and [7,12] share no point.

Input: points = [[1,2],[3,4],[5,6],[7,8]]
Output: 4

No two balloons overlap, so each balloon needs its own arrow.

Input: points = [[1,2],[2,3],[3,4],[4,5]]
Output: 2

An arrow at x = 2 bursts [1,2] and [2,3] because endpoints count, and an arrow at x = 4 bursts [3,4] and [4,5]. One arrow cannot work since [1,2] and [3,4] do not touch.