Find the Nearest Supply Location in a Road Network

Quick Overview

Return the minimum number of roads from a named landing location to any supply location in a large undirected, unweighted network. Handle supplies at the start, disconnected regions, multiple nearest supplies, and an empty supply set.

Find the Nearest Supply Location in a Road Network

Company: LinkedIn

Role: DevOps Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem An undirected road network connects named locations. Some locations contain supplies. Given a landing location, return the minimum number of roads required to reach any supply location, or `-1` if none is reachable. ### Function Contract Implement `nearestSupplyDistance(locations, roads, supplyLocations, landing)`. - `roads[i] = [a, b]` connects two locations in both directions. - Return `0` when the landing location itself has supplies. ### Constraints & Assumptions - `1 <= len(locations) <= 200,000`. - `0 <= len(roads) <= 300,000`. - Location names are unique nonempty strings. - Every road endpoint, supply location, and landing location appears in `locations`. - Every road has equal traversal cost. ### Clarifying Questions to Ask - Is distance measured in roads or physical length? Number of roads. - Are roads directed? No. - Can multiple supply locations be tied? Only the distance is returned. - What if no supplies are listed? Return `-1`. ```hint Search in distance order A breadth-first search from the landing location discovers all locations at distance `d` before any at distance `d + 1`. ``` ```hint Stop on the first supply Once a supply location is removed from the BFS queue, its distance is globally minimum in an unweighted graph. ``` ### Example ```text locations = ["L", "A", "B", "C"] roads = [["L", "A"], ["A", "B"], ["L", "C"]] supplyLocations = ["B"] landing = "L" output = 2 ``` ### Evaluation Focus - Handles cycles with a visited set. - Returns immediately for a supplied landing location. - Avoids repeated whole-path copying when only distance is requested. - Runs in `O(v + e)` time and space. ### Extensions to Discuss 1. How would you answer the nearest supply for every possible landing location? 2. What changes when roads have different nonnegative travel times? 3. How would you return the actual route and resolve tied supply locations?

Quick Answer: Return the minimum number of roads from a named landing location to any supply location in a large undirected, unweighted network. Handle supplies at the start, disconnected regions, multiple nearest supplies, and an empty supply set.

|Home/Coding & Algorithms/LinkedIn
LinkedIn logo
LinkedIn
Aug 1, 2026, 12:00 AM
mediumDevOps EngineerOnsiteCoding & Algorithms
1
0

Problem

An undirected road network connects named locations. Some locations contain supplies. Given a landing location, return the minimum number of roads required to reach any supply location, or -1 if none is reachable.

Function Contract

Implement nearestSupplyDistance(locations, roads, supplyLocations, landing).

  • roads[i] = [a, b] connects two locations in both directions.
  • Return 0 when the landing location itself has supplies.

Constraints & Assumptions

  • 1 <= len(locations) <= 200,000 .
  • 0 <= len(roads) <= 300,000 .
  • Location names are unique nonempty strings.
  • Every road endpoint, supply location, and landing location appears in locations .
  • Every road has equal traversal cost.

Clarifying Questions to Ask Guidance

  • Is distance measured in roads or physical length? Number of roads.
  • Are roads directed? No.
  • Can multiple supply locations be tied? Only the distance is returned.
  • What if no supplies are listed? Return -1 .

Example

locations = ["L", "A", "B", "C"]
roads = [["L", "A"], ["A", "B"], ["L", "C"]]
supplyLocations = ["B"]
landing = "L"
output = 2

Evaluation Focus

  • Handles cycles with a visited set.
  • Returns immediately for a supplied landing location.
  • Avoids repeated whole-path copying when only distance is requested.
  • Runs in O(v + e) time and space.

Extensions to Discuss

  1. How would you answer the nearest supply for every possible landing location?
  2. What changes when roads have different nonnegative travel times?
  3. How would you return the actual route and resolve tied supply locations?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...