FastPrepDistance to the Nearest Supply Point
Problems/Distance to the Nearest Supply Point

Distance to the Nearest Supply Point

LinkedIn logoLinkedIn● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in a LinkedIn onsite interview. The report gives locations, the paths between them, the supply locations, and a query for the distance to the nearest supply. It leaves out edge direction, weights, what to do when a place cannot be reached, and the exact function signature. Here, we use the simplest usual graph with no weights. Our version is about 94% the same as the reported question.

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) → int

Examples

Example 1

n = 6roads = [[0,1],[1,2],[2,3],[1,4],[4,5]]supplyPoints = [3,5]landingPoint = 0return = 3

Both 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 = -1

The 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

LinkedIn interview experiences

  • LinkedIn Senior MLE Interview Questions: 1 Experience
  • LinkedIn Staff AI Engineer Phone Screens: 1 Experience
  • LinkedIn Experienced MLE Onsite: 1 Experience
  • LinkedIn Coding with AI Interview: 4 Experiences
  • LinkedIn Experienced Infrastructure SWE Onsite: 1 Experience
All LinkedIn interview questionsSee LinkedIn hiring insights
Saved
class Solution {
    public int nearestSupplyDistance(int n, int[][] roads, int[] supplyPoints, int landingPoint) {
        // Write your code here.
    }
}
Checking account…