Shortest Hop Path Between Machines
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The mistake that sinks a first attempt on this Google OA, reported September 2026, is grabbing the first shortest path BFS finds and calling it done. The problem asks for the lexicographically smallest one, and that changes how you build the answer. It's an unweighted graph with up to 200000 machines and 300000 connections, so plain BFS is the base. If you blank on the tie-breaking part during the live assessment, StealthCoder is the quiet safety net running on your desktop. Know the trick before you open the link.
The problem
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 = 6 connections = [[0,2],[2,5],[0,1],[1,5],[1,3],[3,4]] source = 0 target = 5 return = [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 = 4 connections = [[0,1],[2,3]] source = 0 target = 3 return = [] 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Unweighted shortest path means BFS. The trap is tie-breaking. Standard BFS with parent pointers gives you some shortest path, not the smallest. The clean fix: run BFS from the target to get dist[] for every node. Then walk from the source. At each step, among neighbors with dist equal to current dist minus 1, pick the smallest ID. That greedy walk is guaranteed lexicographically smallest because every candidate still lies on a shortest path. Alternative: sort adjacency lists and BFS from source, but parent-based ordering gets subtle, so the reverse-BFS plus greedy walk is safer. Edge cases: source equals target returns [source], unreachable returns an empty array. Use an adjacency list, not a matrix, at this size. Recursion will blow the stack, so keep it iterative. If the greedy walk step feels fuzzy mid-assessment, StealthCoder can hand you the working code as a hedge.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Shortest Hop Path Between Machines cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Hop Path Between Machines FAQ
What's the trick in the Google shortest hop path problem?+
Run BFS from the target to compute distances, then walk forward from the source. At each node, pick the smallest-ID neighbor whose distance is exactly one less. That gives the lexicographically smallest shortest path without comparing full paths.
Why doesn't normal BFS with parent pointers work?+
It returns one valid shortest path, usually whichever parent reached a node first. That isn't guaranteed to be lexicographically smallest. Example 1 shows it: both [0,1,5] and [0,2,5] are shortest, and you need the one through machine 1.
What edge cases should I test?+
Source equals target, which returns a single-element path. An unreachable target, which returns an empty array. A machine with no connections. Also a graph where many shortest paths tie, to confirm the smallest-ID choice at each step.
How hard is this really?+
Medium. BFS is standard. The lexicographic requirement is the only twist, and it's solved with one extra pass. Complexity is O(n + m) time and space, which fits 200000 nodes and 300000 edges comfortably.
How do I prepare in 48 hours?+
Write BFS on an adjacency list from memory, then practice the distance-from-target plus greedy reconstruction pattern once. Use iterative code, not recursion. Test the two examples by hand, then a tie-heavy graph. That covers nearly everything this problem can throw at you.