Bird
Raised Fist0
Interview PrepintervalsmediumAmazonGoogleFacebook

Minimum Number of Arrows to Burst Balloons

Choose your preparation mode4 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
📋
Problem

Imagine you have a set of balloons floating along a horizontal line, each balloon occupying a segment. You want to shoot arrows vertically to burst all balloons using as few arrows as possible.

You are given an array points where points[i] = [start_i, end_i] represents the horizontal diameter of a balloon. An arrow can be shot vertically along the x-axis at any point x, bursting all balloons whose diameters intersect x. Return the minimum number of arrows that must be shot to burst all balloons.

1 ≤ points.length ≤ 10^5-2^31 ≤ start_i < end_i ≤ 2^31 - 1
Edge cases: Only one balloon → 1 arrowAll balloons non-overlapping → number of balloons arrowsAll balloons fully overlapping → 1 arrow
</>
IDE
def findMinArrowShots(points: list[list[int]]) -> int:public int findMinArrowShots(int[][] points)int findMinArrowShots(vector<vector<int>>& points)function findMinArrowShots(points)
def findMinArrowShots(points):
    # Write your solution here
    pass
class Solution {
    public int findMinArrowShots(int[][] points) {
        // Write your solution here
        return 0;
    }
}
#include <vector>
using namespace std;

int findMinArrowShots(vector<vector<int>>& points) {
    // Write your solution here
    return 0;
}
function findMinArrowShots(points) {
    // Write your solution here
}
Coming soon
0/9
Common Bugs to Avoid
Wrong: 1Sorting intervals by start coordinate instead of end coordinate causes undercounting arrows.Sort intervals by their end coordinate before applying greedy selection.
Wrong: 3Treating touching intervals as overlapping reduces arrow count incorrectly.Use strict inequality (start > last arrow position) to detect overlaps.
Wrong: 0Not handling empty input correctly, returning default zero or crashing.Add explicit check for empty input and return 0 arrows.
Wrong: TLEUsing nested loops for overlap checking causes O(n^2) time complexity.Implement sorting by end coordinate and greedy linear scan for O(n log n) time.
Test Cases
t1_01basic
Input{"points":[[1,6],[2,8],[7,12],[10,16]]}
Expected2

Shoot one arrow at x = 6 to burst balloons [2,8] and [1,6], and another arrow at x = 11 to burst balloons [10,16] and [7,12].

t1_02basic
Input{"points":[[1,2],[3,4],[5,6],[7,8]]}
Expected4

All balloons are non-overlapping, so each requires a separate arrow.

t2_01edge
Input{"points":[]}
Expected0

No balloons means no arrows needed.

t2_02edge
Input{"points":[[5,10]]}
Expected1

Only one balloon requires exactly one arrow.

t2_03edge
Input{"points":[[1,5],[5,10],[10,15]]}
Expected2

Balloons touch at boundaries but do not overlap; each needs separate arrow.

t3_01corner
Input{"points":[[1,10],[2,3],[4,5],[6,7],[8,9]]}
Expected1

All smaller balloons are fully inside the first balloon, so one arrow at end=10 bursts all.

t3_02corner
Input{"points":[[1,2],[2,3],[3,4],[4,5]]}
Expected2

Balloons only touch at boundaries, no overlaps; each needs separate arrow.

t3_03corner
Input{"points":[[-10,-5],[-7,-3],[-6,-1],[-2,0]]}
Expected2

Handles negative coordinates correctly; arrows at -5 and 0 burst all balloons.

t4_01performance
Input{"_description":"n=100000 at constraint boundary - executor generates this"}
⏱ Performance - must finish in 2000ms

n=100000, O(n log n) sorting and O(n) greedy must complete within 2 seconds.

Practice

(1/5)
1. You are given a list of meeting time intervals represented as pairs of start and end times. You need to determine if a single person can attend all meetings without any overlaps. Which approach guarantees an optimal and efficient solution?
easy
A. Use dynamic programming to find the maximum number of non-overlapping intervals.
B. Sort intervals by start time and check for any overlap by comparing each interval's start with the previous interval's end.
C. Use a brute force nested loop to compare every pair of intervals for overlap.
D. Sort intervals by end time and greedily select intervals that finish earliest to maximize the number of meetings attended.

Solution

  1. Step 1: Understand the problem requirement

    The problem asks if a single person can attend all meetings without overlap, so we must check if any intervals overlap.
  2. Step 2: Identify the correct approach

    Sorting intervals by start time and checking if any interval starts before the previous one ends guarantees detection of overlaps efficiently.
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Sorting by start time and checking overlaps is the standard approach [OK]
Hint: Sort by start time and check overlaps [OK]
Common Mistakes:
  • Confusing sorting by end time with start time for overlap detection
2. What is the time complexity of the optimal car pooling solution using the difference array and prefix sum approach, given n trips and maximum location L?
medium
A. O(n * L) because we must simulate passenger count at every location for each trip
B. O(n + L) because we process each trip once and then sweep through the difference array once
C. O(n log n) due to sorting trips by start location before processing
D. O(L) because the difference array size dominates and trips are processed in constant time

Solution

  1. Step 1: Analyze trip processing

    Each of the n trips updates two positions in the difference array -> O(n)
  2. Step 2: Analyze prefix sum sweep

    We iterate over the difference array of size L once -> O(L)
  3. Final Answer:

    Option B -> Option B
  4. Quick Check:

    Sum of O(n) + O(L) operations [OK]
Hint: Difference array updates O(n), prefix sum O(L) [OK]
Common Mistakes:
  • Confusing brute force O(n*L) with optimal
  • Assuming sorting is required
  • Ignoring prefix sum sweep cost
3. What is the time complexity of the optimal in-place merge intervals algorithm that first sorts the intervals and then merges them in one pass?
medium
A. O(n log n) due to the sorting step dominating the runtime
B. O(n) because merging is done in a single pass
C. O(n²) because each interval is compared with all others during merging
D. O(n log n) because merging requires binary searches for overlaps

Solution

  1. Step 1: Identify the sorting step complexity.

    Sorting n intervals by start time takes O(n log n) time.
  2. Step 2: Analyze the merging step complexity.

    Merging intervals in one pass is O(n), which is dominated by sorting.
  3. Final Answer:

    Option A -> Option A
  4. Quick Check:

    Sorting dominates overall complexity, merging is linear. [OK]
Hint: Sorting dominates, merging is linear [OK]
Common Mistakes:
  • Ignoring sorting cost
  • Assuming nested comparisons cause O(n²)
4. Examine the following buggy code snippet for the minimum interval problem. Which line contains the subtle bug that can cause incorrect results on some inputs?
def min_interval_binary_search(intervals, queries):
    intervals.sort(key=lambda x: x[1] - x[0] + 1)
    lengths = [end - start for start, end in intervals]
    starts = [start for start, end in intervals]
    ends = [end for start, end in intervals]

    def covers(query, length):
        idx = bisect.bisect_right(lengths, length)
        for i in range(idx):
            if starts[i] <= query <= ends[i]:
                return True
        return False

    res = []
    max_len = lengths[-1] if lengths else 0
    for q in queries:
        left, right = 1, max_len
        ans = -1
        while left <= right:
            mid = (left + right) // 2
            if covers(q, mid):
                ans = mid
                right = mid - 1
            else:
                left = mid + 1
        res.append(ans)
    return res
medium
A. Line 15: max_len = lengths[-1] if lengths else 0
B. Line 2: intervals.sort(key=lambda x: x[1] - x[0] + 1)
C. Line 9: for i in range(idx):
D. Line 3: lengths = [end - start for start, end in intervals]

Solution

  1. Step 1: Check interval length calculation

    Line 3 calculates lengths as end - start, missing the +1 needed for inclusive intervals.
  2. Step 2: Impact of bug

    Incorrect lengths cause wrong sorting order and coverage checks, leading to wrong minimal interval lengths.
  3. Final Answer:

    Option D -> Option D
  4. Quick Check:

    Correct interval length must be end - start + 1 [OK]
Hint: Check interval length calculation carefully [OK]
Common Mistakes:
  • Off-by-one in length calculation
  • Not sorting intervals
  • Incorrect binary search boundaries
5. Suppose the problem is modified so that intervals can be reused multiple times for different queries, and queries can be negative integers. Which modification to the binary search + preprocessing approach is necessary to handle this variant correctly and efficiently?
hard
A. Preprocess intervals by length and use a segment tree to query coverage efficiently for negative and repeated queries.
B. No change needed; the original approach works for reuse and negative queries.
C. Sort queries and intervals, use a min-heap to maintain active intervals covering queries in order.
D. Use brute force checking each interval for each query since reuse breaks binary search assumptions.

Solution

  1. Step 1: Identify challenges with reuse and negative queries

    Negative queries require data structures supporting arbitrary ranges; reuse means intervals must be efficiently queried multiple times.
  2. Step 2: Choose appropriate data structure

    Segment tree or interval tree allows efficient coverage queries over any integer range and supports multiple queries efficiently.
  3. Step 3: Why other options fail

    Min-heap approach requires sorting queries and intervals but doesn't handle negative queries well; brute force is inefficient; original approach assumes positive queries and no reuse.
  4. Final Answer:

    Option A -> Option A
  5. Quick Check:

    Segment tree supports arbitrary queries and reuse efficiently [OK]
Hint: Segment tree handles arbitrary queries and reuse [OK]
Common Mistakes:
  • Assuming original approach works unchanged
  • Using min-heap without sorting queries
  • Resorting to brute force unnecessarily