FastPrepShortest Hop Path Between Machines
Problems/Shortest Hop Path Between Machines

Shortest Hop Path Between Machines

Google logoGoogle● MediumINTERNONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: The report gives a partial graph of machine connections, a path from source to target with the fewest hops, rebuilding the path, BFS, and follow-ups on scale, cycles, and memory. It does not give edge direction, how to order ties, what to do when unreachable, the function inputs, or the input limits. Our version is about 90% the same as the reported question.

A data center contains n machines numbered from 0 through n - 1. Each pair connections[i] = [u, v] is a bidirectional connection between machines u and v. Every connection takes one hop.

Return a path from source to target that uses the minimum possible number of hops, including both endpoints. If several shortest paths exist, return the lexicographically smallest machine sequence. If the target is unreachable, return an empty array.

When source == target, return the one-machine path [source].

Function

shortestMachinePath(n: int, connections: int[][], source: int, target: int) → int[]

Examples

Example 1

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

Paths [0, 1, 5] and [0, 2, 5] both use two hops. The path through machine 1 is lexicographically smaller.

Example 2

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

The source and target lie in different connected components, so no path exists.

Constraints

  • 1 <= n <= 200000.
  • 0 <= connections.length <= 300000.
  • Every connection contains two distinct machine IDs in [0, n - 1].
  • No undirected connection appears more than once.
  • 0 <= source, target < n.

More Google Onsite Interview questions

  • Route Pattern MatcherPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Evaluate a Nested Math ExpressionPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Distinct Subset SumsONSITE INTERVIEW · Seen Nov 2026
  • Playlist With No Same Artist in a RowONSITE INTERVIEW · Seen Nov 2026
  • Delete Nodes and Return Forest RootsONSITE INTERVIEW · Seen Nov 2026
  • Dice-Walk Winning ProbabilityPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • First and Last Target Position in a Mountain ArrayONSITE INTERVIEW · Seen Nov 2026
  • Lowest Common Ancestor in an N-ary TreePHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
  • Batched Subsequence Word MatchesONSITE INTERVIEW · Seen Nov 2026

Similar questions at other companies

  • Shortest Increasing Path to a TargetUber · ONSITE INTERVIEWUber interview questions
  • Find and Reconstruct a Binary-Matrix PathMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Shortest Distance from All BuildingsWaymo · PHONE SCREENWaymo interview questions
  • Cheapest Flights Within K StopsAmazon · PHONE SCREEN · ONSITE INTERVIEW
  • Fire Station Response BillLinkedIn · ONSITE INTERVIEWLinkedIn interview questions
  • Island Count and Maximum AreaOracle · ONSITE INTERVIEWOracle interview questions
  • Minimum Friend API CallsMercor · ONSITE INTERVIEWMercor interview questions
  • Cheapest Flights Within K StopsGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
  • Open the Lock and Return a Shortest PathZip · PHONE SCREEN · ONSITE INTERVIEWZip interview questions
  • Max Area of IslandRoku · ONSITE INTERVIEWRoku interview questions

Interview reports that mention this question

  • Google Intern Team Matching: 14 Experiences (Part 4 of 8)
  • Google Intern Prefix Sums and Heaps: 6 Experiences (Part 3 of 8)
  • Google SWE Intern Interviews: 85 Experiences (Part 1 of 8)

Google interview experiences

  • Google interviews · November 2026
  • Google Software Engineer Onsite Questions: 10 Experiences
  • Google OA: What It Tests (2026)
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public int[] shortestMachinePath(int n, int[][] connections, int source, int target) {
        // Write your code here.
    }
}
Checking account…