Count buildings covered by at least one router's signal range

Quick Overview

Given buildings on a line and routers with signal ranges, count how many buildings receive signal from at least one router. Tests clipping intervals to bounds, the difference array technique with prefix sums, and counting a union of overlapping ranges in linear time.

Count buildings covered by at least one router's signal range

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given `n` buildings numbered from `1` to `n` arranged in a line. Several routers are placed on specific buildings, each with a signal range. A router placed on building `p` with range `r` covers all buildings from `max(1, p - r)` to `min(n, p + r)` inclusive. Given the number of buildings `n` and a list of `routers` where `routers[i] = [p_i, r_i]` represents a router at position `p_i` with range `r_i`, return the number of buildings that receive signal coverage from at least one router. ### Function Signature ```python def count_covered_buildings(n: int, routers: list[list[int]]) -> int: ``` ### Rules - Buildings are numbered from 1, and both ends of a router's coverage are inclusive. - Coverage is clipped to the street: a range that reaches past building 1 or building `n` stops there. - A building covered by several routers counts once. ### Constraints - `1 <= n <= 10^5` - `1 <= len(routers) <= 10^5` - `1 <= routers[i][0] <= n` - `0 <= routers[i][1] <= n` ### Examples ```text Input: n = 10, routers = [[3, 2], [7, 1]] Output: 8 ``` The router at building 3 with range 2 covers buildings 1 through 5, and the router at building 7 with range 1 covers buildings 6 through 8. Together they cover buildings 1 through 8, which is 8 buildings. ```text Input: n = 5, routers = [[1, 0], [5, 0]] Output: 2 ``` A range of 0 covers only the router's own building, so buildings 1 and 5 are covered and the other three are not. ```text Input: n = 6, routers = [[2, 3], [5, 0], [4, 1]] Output: 5 ``` The first router would reach from building -1 to 5, which clips to 1 through 5. The other two cover building 5 and buildings 3 through 5, both inside that range. Building 6 is 4 away from the first router, 1 away from the second (range 0) and 2 away from the third (range 1), so it is the only uncovered building.

Overview: Given buildings on a line and routers with signal ranges, count how many buildings receive signal from at least one router. Tests clipping intervals to bounds, the difference array technique with prefix sums, and counting a union of overlapping ranges in linear time.

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

You are given n buildings numbered from 1 to n arranged in a line. Several routers are placed on specific buildings, each with a signal range.

A router placed on building p with range r covers all buildings from max(1, p - r) to min(n, p + r) inclusive.

Given the number of buildings n and a list of routers where routers[i] = [p_i, r_i] represents a router at position p_i with range r_i, return the number of buildings that receive signal coverage from at least one router.

Function Signature

def count_covered_buildings(n: int, routers: list[list[int]]) -> int:

Rules

  • Buildings are numbered from 1, and both ends of a router's coverage are inclusive.
  • Coverage is clipped to the street: a range that reaches past building 1 or building n stops there.
  • A building covered by several routers counts once.

Constraints

  • 1 <= n <= 10^5
  • 1 <= len(routers) <= 10^5
  • 1 <= routers[i][0] <= n
  • 0 <= routers[i][1] <= n

Examples

Input: n = 10, routers = [[3, 2], [7, 1]]
Output: 8

The router at building 3 with range 2 covers buildings 1 through 5, and the router at building 7 with range 1 covers buildings 6 through 8. Together they cover buildings 1 through 8, which is 8 buildings.

Input: n = 5, routers = [[1, 0], [5, 0]]
Output: 2

A range of 0 covers only the router's own building, so buildings 1 and 5 are covered and the other three are not.

Input: n = 6, routers = [[2, 3], [5, 0], [4, 1]]
Output: 5

The first router would reach from building -1 to 5, which clips to 1 through 5. The other two cover building 5 and buildings 3 through 5, both inside that range. Building 6 is 4 away from the first router, 1 away from the second (range 0) and 2 away from the third (range 1), so it is the only uncovered building.