Check whether a message can hop between WiFi routers whose signal ranges touch or overlap

Quick Overview

Decide whether a message can travel from one WiFi router to another when two routers connect only if their signal circles touch or overlap. It tests turning geometry into an implicit graph, exact integer distance checks and breadth-first search for reachability.

Check whether a message can hop between WiFi routers whose signal ranges touch or overlap

Company: Fitbit

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given a list of WiFi routers, where `routers[i] = [x, y, r]` represents the position `(x, y)` and the signal range radius `r` of the i-th router. Two routers are directly connected if the Euclidean distance between them is less than or equal to the sum of their individual signal ranges. Given a `source` index and a `target` index, determine whether a message can travel from `source` to `target` through a chain of router broadcasts. Return `True` if the target is reachable from the source, `False` otherwise. ### Function Signature ```python def can_reach_router(routers: list[list[int]], source: int, target: int) -> bool: ``` ### Rules - Routers `i` and `j` are directly connected when $\sqrt{(x_i - x_j)^2 + (y_i - y_j)^2} \le r_i + r_j$. Equality counts, so two ranges that only touch are connected. - The connection is symmetric: if `i` can reach `j` directly, `j` can reach `i` directly. - A message can pass along any chain of directly connected routers, with any number of hops. - If `source == target`, the message is already there and the answer is `True`. ### Constraints - `1 <= len(routers) <= 1000` - `routers[i].length == 3`, where each entry is `[x, y, r]` - `-10000 <= x, y <= 10000` - `0 <= r <= 10000` - `0 <= source, target < len(routers)` ### Examples ```text Input: routers = [[0,0,2],[3,0,2],[10,0,1],[11,0,1]], source = 0, target = 3 Output: False ``` R0 and R1 connect (distance 3 is at most 2 + 2 = 4), and R2 and R3 connect (distance 1 is at most 1 + 1 = 2). The closest pair across the two groups is R1 and R2 at distance 7, which exceeds 2 + 1 = 3, so the groups are isolated and router 3 is unreachable from router 0. ```text Input: routers = [[0,0,1],[5,0,1],[10,0,1]], source = 0, target = 2 Output: False ``` Each adjacent pair is 5 apart, but the combined radius is only 2, so no router is connected to any other. ```text Input: routers = [[0,0,2],[3,4,3],[9,4,3]], source = 0, target = 2 Output: True ``` R0 and R1 are 5 apart with combined radius 5, and R1 and R2 are 6 apart with combined radius 6, so both pairs touch exactly and connect. R0 and R2 are about 9.85 apart, more than their combined radius of 5, so the message has to go through R1.

Overview: Decide whether a message can travel from one WiFi router to another when two routers connect only if their signal circles touch or overlap. It tests turning geometry into an implicit graph, exact integer distance checks and breadth-first search for reachability.

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

You are given a list of WiFi routers, where routers[i] = [x, y, r] represents the position (x, y) and the signal range radius r of the i-th router.

Two routers are directly connected if the Euclidean distance between them is less than or equal to the sum of their individual signal ranges.

Given a source index and a target index, determine whether a message can travel from source to target through a chain of router broadcasts. Return True if the target is reachable from the source, False otherwise.

Function Signature

def can_reach_router(routers: list[list[int]], source: int, target: int) -> bool:

Rules

  • Routers i and j are directly connected when (xi−xj)2+(yi−yj)2≤ri+rj\sqrt{(x_i - x_j)^2 + (y_i - y_j)^2} \le r_i + r_j . Equality counts, so two ranges that only touch are connected.
  • The connection is symmetric: if i can reach j directly, j can reach i directly.
  • A message can pass along any chain of directly connected routers, with any number of hops.
  • If source == target , the message is already there and the answer is True .

Constraints

  • 1 <= len(routers) <= 1000
  • routers[i].length == 3 , where each entry is [x, y, r]
  • -10000 <= x, y <= 10000
  • 0 <= r <= 10000
  • 0 <= source, target < len(routers)

Examples

Input: routers = [[0,0,2],[3,0,2],[10,0,1],[11,0,1]], source = 0, target = 3
Output: False

R0 and R1 connect (distance 3 is at most 2 + 2 = 4), and R2 and R3 connect (distance 1 is at most 1 + 1 = 2). The closest pair across the two groups is R1 and R2 at distance 7, which exceeds 2 + 1 = 3, so the groups are isolated and router 3 is unreachable from router 0.

Input: routers = [[0,0,1],[5,0,1],[10,0,1]], source = 0, target = 2
Output: False

Each adjacent pair is 5 apart, but the combined radius is only 2, so no router is connected to any other.

Input: routers = [[0,0,2],[3,4,3],[9,4,3]], source = 0, target = 2
Output: True

R0 and R1 are 5 apart with combined radius 5, and R1 and R2 are 6 apart with combined radius 6, so both pairs touch exactly and connect. R0 and R2 are about 9.85 apart, more than their combined radius of 5, so the message has to go through R1.