Distance to the Nearest Supply Point
Updated
Problem statement
There are n locations numbered from 0 through n - 1. The undirected array roads contains the available connections, where roads[i] = [u, v] joins two locations and traversing one road has distance 1.
Some locations in supplyPoints contain supplies. Given a landingPoint, return the minimum number of roads needed to reach any supply point. Return -1 if no supply point is reachable.
Function
nearestSupplyDistance(n: int, roads: int[][], supplyPoints: int[], landingPoint: int) → intExamples
Example 1
n = 6roads = [[0,1],[1,2],[2,3],[1,4],[4,5]]supplyPoints = [3,5]landingPoint = 0return = 3Both supply locations are three roads from location 0, so the nearest distance is 3.
Example 2
n = 5roads = [[0,1],[1,2],[3,4]]supplyPoints = [4]landingPoint = 0return = -1The landing point and the only supply point are in different connected components.
Constraints
1 <= n <= 200000.0 <= roads.length <= 200000.- Every road endpoint, supply point, and the landing point is a valid location.
- Supply points are unique.
- Parallel roads do not change the answer.
More LinkedIn Onsite Interview questions
- Allocate Mailboxes with Squared DistanceONSITE INTERVIEW · Seen Nov 2026
- Find Leaves of a Binary TreeONSITE INTERVIEW · Seen Nov 2026
- Max Points on a LineONSITE INTERVIEW · Seen Nov 2026
- Explore an Unknown Grid and Find a PathONSITE INTERVIEW · Seen Nov 2026
- Fire Station Response BillONSITE INTERVIEW · Seen Nov 2026
- Roll a Weighted DieONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Subarray With at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- Kth Closest Element in a BSTONSITE INTERVIEW · Seen Oct 2026
- Longest Binary Window with at Most K OnesONSITE INTERVIEW · Seen Oct 2026
- LRU Cache OperationsONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Shortest Path Node Count Avoiding Broken NodesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
- Minimum Friend API CallsMercor · ONSITE INTERVIEWMercor interview questions
- Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
- Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Open the Lock and Return a Shortest PathZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
- Shortest Word Chain Between Two WordsFanatics · PHONE SCREEN
- Shortest Travel Time in a Grid with Traffic LightsWaymo · PHONE SCREENWaymo interview questions
- Delivery Management SystemAkuna Capital · OAAkuna Capital interview questions
- Shortest Path from an Encoded GraphIntuit · OAIntuit interview questions