Reach a destination using only edges whose endpoint weights differ by more than a threshold

Quick Overview

Decide whether a destination node can be reached in an undirected graph when an edge may be crossed only if the weights of its two endpoints differ by more than a given threshold. It tests graph reachability with an edge filter, strict comparisons, and edge cases such as a source that equals the destination.

Reach a destination using only edges whose endpoint weights differ by more than a threshold

Company: Uber

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: easy

Interview Round: Other

You are given an undirected graph with `n` nodes labeled from `0` to `n - 1`. Each node has a weight given in the array `weights`, where `weights[i]` is the weight of node `i`. You are also given a list of `edges` where `edges[i] = [uᵢ, vᵢ]` indicates an undirected edge between nodes `uᵢ` and `vᵢ`. An edge between two directly connected nodes `u` and `v` can only be traversed if the absolute difference of their weights **strictly exceeds** the given `threshold`. That is, the edge `(u, v)` is usable only if `|weights[u] - weights[v]| > threshold`. Given a `source` node and a `destination` node, return `True` if there exists a path from `source` to `destination` using only traversable edges, or `False` otherwise. ### Function Signature ```python def can_reach_with_threshold(n: int, edges: list[list[int]], weights: list[int], threshold: int, source: int, destination: int) -> bool: ``` ### Rules - Edges are undirected. A usable edge `[u, v]` can be crossed from `u` to `v` or from `v` to `u`. - The weights belong to the nodes. An edge has no weight of its own; its usability depends only on the weights of its two endpoints. - The comparison is strict. A difference exactly equal to `threshold` blocks the edge. - If `source == destination`, return `True`. The path with no edges counts. ### Constraints - `2 <= n <= 10^4` - `len(weights) == n` - `0 <= len(edges) <= 10^4` - `len(edges[i]) == 2` - `0 <= edges[i][0], edges[i][1] < n` - `edges[i][0] != edges[i][1]` - `0 <= weights[i] <= 10^6` - `0 <= threshold <= 10^6` - `0 <= source, destination < n` - No duplicate edges exist. ### Examples ```text Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4],[0,4]], weights = [10,50,20,80,30], threshold = 5, source = 0, destination = 3 Output: True ``` The path `0 -> 1 -> 2 -> 3` is valid because the weight differences along it are 40, 30 and 60, all greater than 5. The route `0 -> 4 -> 3` (differences 20 and 50) would also work. ```text Input: n = 4, edges = [[0,1],[1,2],[2,3]], weights = [10,12,14,100], threshold = 5, source = 0, destination = 3 Output: False ``` The only edge at node 0 goes to node 1, and `|10 - 12| = 2` is not greater than 5, so node 0 cannot move anywhere and node 3 is out of reach. ```text Input: n = 2, edges = [], weights = [5,5], threshold = 0, source = 1, destination = 1 Output: True ``` The source is already the destination, so no edge is needed.

Overview: Decide whether a destination node can be reached in an undirected graph when an edge may be crossed only if the weights of its two endpoints differ by more than a given threshold. It tests graph reachability with an edge filter, strict comparisons, and edge cases such as a source that equals the destination.

Uber logo
Uber
Nov 24, 2026
easySoftware EngineerOtherCoding & Algorithms
0
0

You are given an undirected graph with n nodes labeled from 0 to n - 1. Each node has a weight given in the array weights, where weights[i] is the weight of node i. You are also given a list of edges where edges[i] = [uᵢ, vᵢ] indicates an undirected edge between nodes uᵢ and vᵢ.

An edge between two directly connected nodes u and v can only be traversed if the absolute difference of their weights strictly exceeds the given threshold. That is, the edge (u, v) is usable only if |weights[u] - weights[v]| > threshold.

Given a source node and a destination node, return True if there exists a path from source to destination using only traversable edges, or False otherwise.

Function Signature

def can_reach_with_threshold(n: int, edges: list[list[int]], weights: list[int], threshold: int, source: int, destination: int) -> bool:

Rules

  • Edges are undirected. A usable edge [u, v] can be crossed from u to v or from v to u .
  • The weights belong to the nodes. An edge has no weight of its own; its usability depends only on the weights of its two endpoints.
  • The comparison is strict. A difference exactly equal to threshold blocks the edge.
  • If source == destination , return True . The path with no edges counts.

Constraints

  • 2 <= n <= 10^4
  • len(weights) == n
  • 0 <= len(edges) <= 10^4
  • len(edges[i]) == 2
  • 0 <= edges[i][0], edges[i][1] < n
  • edges[i][0] != edges[i][1]
  • 0 <= weights[i] <= 10^6
  • 0 <= threshold <= 10^6
  • 0 <= source, destination < n
  • No duplicate edges exist.

Examples

Input: n = 5, edges = [[0,1],[1,2],[2,3],[3,4],[0,4]], weights = [10,50,20,80,30], threshold = 5, source = 0, destination = 3
Output: True

The path 0 -> 1 -> 2 -> 3 is valid because the weight differences along it are 40, 30 and 60, all greater than 5. The route 0 -> 4 -> 3 (differences 20 and 50) would also work.

Input: n = 4, edges = [[0,1],[1,2],[2,3]], weights = [10,12,14,100], threshold = 5, source = 0, destination = 3
Output: False

The only edge at node 0 goes to node 1, and |10 - 12| = 2 is not greater than 5, so node 0 cannot move anywhere and node 3 is out of reach.

Input: n = 2, edges = [], weights = [5,5], threshold = 0, source = 1, destination = 1
Output: True

The source is already the destination, so no edge is needed.