FastPrepShortest Distance Between Two Nodes
Problems/Shortest Distance Between Two Nodes

Shortest Distance Between Two Nodes

Scale AI logoScale AI● MediumNEW GRADONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: An onsite report asks for a graph constructed from a Python dictionary and the shortest distance between two nodes. A separate onsite tree report names node-distance questions but omits its later questions. This practice supplies an indexed, undirected unit-edge graph, one scalar query, bounds and disconnected behavior; the original dictionary schemas are unavailable. Editorial estimate: about 70% agreement with the reported main capability.

For this exercise, an undirected graph has n vertices labelled 0 through n-1. Each row [u,v] in edges joins u and v in both directions and has length one.

Return the minimum number of edges in any path from source to target. Return 0 if they are the same vertex, and -1 if no path connects them. Declared vertices with no incident edges still exist.

There are no self-loops or duplicate undirected edges. Cycles and disconnected components are allowed.

Function

shortestNodeDistance(n: int, edges: int[][], source: int, target: int) → int

Examples

Example 1

n = 5edges = [[0,1],[1,2],[2,3],[0,4],[4,3]]source = 0target = 3return = 2

The path 0 → 4 → 3 uses two edges, shorter than 0 → 1 → 2 → 3.

Example 2

n = 4edges = [[0,1]]source = 0target = 3return = -1

Vertex 3 is isolated and cannot be reached from vertex 0.

Example 3

n = 1edges = []source = 0target = 0return = 0

A vertex is zero edges away from itself, including an isolated vertex.

Constraints

  • For this exercise, assume 1 <= n <= 500 and 0 <= edges.length <= 1000.
  • Each edge has exactly two valid, different indices in [0,n-1]. Every undirected edge occurs once, in either orientation.
  • source and target are valid indices. All supplied inputs satisfy these rules.

More Scale AI Onsite Interview questions

  • Task Scheduler with DependenciesONSITE INTERVIEW · Seen Jul 2026
  • Best Poker Category with a JokerPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2026
  • Format and Validate a Model ResponseONSITE INTERVIEW · Seen Apr 2026
  • Stateful Neuron Matrix UpdateONSITE INTERVIEW · Seen Apr 2026
  • Clock Hand Angle with Subsecond PrecisionONSITE INTERVIEW · Seen Dec 2025
  • Four-Player Matching Card GameONSITE INTERVIEW · Seen Feb 2025
  • Rotate an Array to the RightONSITE INTERVIEW · Seen Oct 2024
  • Rich-Text Segment Normalization and Bold ToggleONSITE INTERVIEW · Seen Apr 2024
  • Minimum Distance Matching in a ForestPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2024
  • Fit Beta Parameters by Maximum LikelihoodONSITE INTERVIEW · Seen Sep 2022

Similar questions at other companies

  • Course ScheduleAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Course ScheduleOracle · ONSITE INTERVIEWOracle interview questions
  • Course SchedulePlaud · ONSITE INTERVIEW
  • Explore an Unknown Grid and Find a PathLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Nearest Charging Station on a GridSpotify · ONSITE INTERVIEW
  • Hierarchical Permission CheckeBay · ONSITE INTERVIEWeBay interview questions
  • Shortest Path Node Count Avoiding Broken NodesGoogle · PHONE SCREEN · ONSITE INTERVIEWGoogle interview questions
  • Evaluate DivisionPhonePe · ONSITE INTERVIEWPhonePe interview questions
  • Count Friend CirclesOpenAI · PHONE SCREEN · ONSITE INTERVIEWOpenAI interview questions
  • Finite Web CrawlerMongoDB · PHONE SCREEN · ONSITE INTERVIEWMongoDB interview questions

Interview reports that mention this question

  • Scale AI New Grad SWE and MLE Question Bank
  • Scale AI New Grad ML Research Engineer LLM Onsite: 1 Experience
  • Scale AI New Grad MLRE Back-to-Back Onsite: 1 Experience
  • Scale AI New Grad ML Research Engineer Onsite: 1 Experience

Scale AI interview experiences

  • Scale AI OA: What It Tests (2026)
  • Scale AI Forward Deployed Engineer Debugging Round: 1 Experience
All Scale AI interview questionsSee Scale AI hiring insights
Saved
class Solution {
    public int shortestNodeDistance(int n, int[][] edges, int source, int target) {
        // write your code here
    }
}
Checking account…