Find the fewest satellite beams that cover a strip from 0 to target

Quick Overview

Given satellite beams as intervals on a line, find the fewest beams that together cover every point from 0 to a target position, or return -1 when a gap makes that impossible. It tests greedy interval covering, sorting, and a careful argument for why the greedy choice is optimal.

Find the fewest satellite beams that cover a strip from 0 to target

Company: Spacex

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are planning satellite beam coverage over a one-dimensional strip of terrain. Each satellite beam covers a contiguous interval on the terrain. Given `n` satellite beams, each described by a start and end position `[start, end]`, determine the minimum number of beams needed to cover the entire terrain from position `0` to position `target`. If it is not possible to cover the entire terrain, return `-1`. ### Function Signature ```python def min_beams(beams: list[list[int]], target: int) -> int: ``` ### Rules - `beams[i] = [start, end]` covers every point `x` with `start <= x <= end`. - Positions are points on a continuous line. Two beams that share an endpoint, such as `[0, 4]` and `[4, 8]`, cover `[0, 8]` with no gap. The beams `[0, 4]` and `[5, 8]` leave the stretch between 4 and 5 uncovered. - Chosen beams may overlap each other and may extend past `target`. Only the stretch from `0` to `target` has to be covered. - The beams are given in no particular order. - Return only the count, or `-1` when no selection of beams covers `[0, target]`. ### Constraints - `1 <= beams.length <= 10^4` - `0 <= beams[i][0] < beams[i][1] <= 10^5` - `1 <= target <= 10^5` ### Examples ```text Input: beams = [[0,4],[2,7],[5,10],[8,12]], target = 12 Output: 4 ``` Only `[0,4]` starts at 0. From 4 the only beam that continues the coverage is `[2,7]`, from 7 it is `[5,10]`, and from 10 it is `[8,12]`, so all four beams are needed. ```text Input: beams = [[3,7],[0,2],[6,10],[1,4],[0,6]], target = 10 Output: 2 ``` `[0,6]` and `[6,10]` meet at position 6 and together cover 0 to 10. No single beam spans the whole strip, so 2 is the minimum. ```text Input: beams = [[0,5],[1,3],[6,9]], target = 9 Output: -1 ``` No beam covers the points between 5 and 6, so the strip cannot be covered.

Overview: Given satellite beams as intervals on a line, find the fewest beams that together cover every point from 0 to a target position, or return -1 when a gap makes that impossible. It tests greedy interval covering, sorting, and a careful argument for why the greedy choice is optimal.

Spacex logo
Spacex
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are planning satellite beam coverage over a one-dimensional strip of terrain. Each satellite beam covers a contiguous interval on the terrain.

Given n satellite beams, each described by a start and end position [start, end], determine the minimum number of beams needed to cover the entire terrain from position 0 to position target.

If it is not possible to cover the entire terrain, return -1.

Function Signature

def min_beams(beams: list[list[int]], target: int) -> int:

Rules

  • beams[i] = [start, end] covers every point x with start <= x <= end .
  • Positions are points on a continuous line. Two beams that share an endpoint, such as [0, 4] and [4, 8] , cover [0, 8] with no gap. The beams [0, 4] and [5, 8] leave the stretch between 4 and 5 uncovered.
  • Chosen beams may overlap each other and may extend past target . Only the stretch from 0 to target has to be covered.
  • The beams are given in no particular order.
  • Return only the count, or -1 when no selection of beams covers [0, target] .

Constraints

  • 1 <= beams.length <= 10^4
  • 0 <= beams[i][0] < beams[i][1] <= 10^5
  • 1 <= target <= 10^5

Examples

Input: beams = [[0,4],[2,7],[5,10],[8,12]], target = 12
Output: 4

Only [0,4] starts at 0. From 4 the only beam that continues the coverage is [2,7], from 7 it is [5,10], and from 10 it is [8,12], so all four beams are needed.

Input: beams = [[3,7],[0,2],[6,10],[1,4],[0,6]], target = 10
Output: 2

[0,6] and [6,10] meet at position 6 and together cover 0 to 10. No single beam spans the whole strip, so 2 is the minimum.

Input: beams = [[0,5],[1,3],[6,9]], target = 9
Output: -1

No beam covers the points between 5 and 6, so the strip cannot be covered.