Distances to the Nearest Infected Node
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The Google OA reported in October 2024 looks like a graph problem with a lot of nodes, but it reduces to one thing: multi-source BFS. Every infected node starts at distance 0, and you spread outward one edge at a time. If you try to run a search from each node, you'll time out at 200000 nodes. Seeing the single-queue idea is the whole question. If you blank on it mid-assessment, StealthCoder runs invisibly on your screen and gives you the working approach in real time.
The problem
You are given an undirected, unweighted graph with n nodes numbered from 0 to n - 1. The array infected contains the distinct nodes that are already infected. Return an integer array distance of length n, where distance[v] is the minimum number of edges on a path from node v to any infected node. Return -1 for a node that cannot reach any infected node. Function nearestInfectedDistances(n: int, edges: int[][], infected: int[]) → int[] Examples Example 1 n = 7 edges = [[0,1],[1,2],[2,3],[4,5]] infected = [0,3,5] return = [0,1,1,0,1,0,-1] Nodes 0, 3, and 5 start at distance 0. Nodes 1, 2, and 4 are one edge from an infected node. Node 6 is isolated, so its distance is -1. Example 2 n = 5 edges = [[0,1],[1,2],[2,3],[3,4]] infected = [1,4] return = [1,0,1,1,0] A shortest path may end at either infected source. Node 3, for example, is one edge from node 4. Constraints 1 <= n <= 200000 0 <= edges.length <= 200000 Every row of edges is [u, v] with 0 <= u, v < n and u != v. 1 <= infected.length <= n All entries of infected are distinct valid node indices.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: don't compute distance per node. Build an adjacency list, create a distance array filled with -1, and push every infected node into the queue with distance 0 at the start. Then run a normal BFS. The first time a node gets reached is its shortest distance, because BFS expands in layers. Nodes never reached keep -1, which handles the isolated node in Example 1 for free. The common pitfall is running BFS from each node, which is O(n * (n + m)) and dies on the constraints. Another is using a visited set separate from the distance array, which is fine but redundant. Also remember edges are undirected, so add both directions. Use an index pointer or deque so pops are O(1). Total cost is O(n + m). StealthCoder is your hedge if the multi-source framing escapes you during the live OA.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Distances to the Nearest Infected Node 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Distances to the Nearest Infected Node FAQ
What's the trick in the Google nearest infected node problem?+
Treat all infected nodes as a single starting layer. Put them all in the BFS queue at distance 0, then expand. Each node's first visit gives its minimum distance to any infected node. One pass, O(n + m), no per-node searches.
How hard is this OA question really?+
Medium. The code is short once you know multi-source BFS. The difficulty is recognizing it instead of running a BFS from every node. If you've written standard BFS before, the change is only seeding the queue with several nodes.
How do I handle nodes that can't reach any infected node?+
Initialize the distance array to -1 and only overwrite it when BFS reaches a node. Disconnected nodes never get touched, so they stay -1. No special case or final cleanup loop is needed.
Why does BFS give the shortest distance here?+
The graph is unweighted, and BFS processes nodes in order of increasing distance from the sources. The first time a node is discovered is via the fewest edges. Since all sources start together at 0, that minimum is across all infected nodes.
How do I prepare for this in 48 hours?+
Write multi-source BFS from scratch twice. Practice building an adjacency list from an edge array, seeding the queue, and using the distance array as the visited marker. Test on an isolated node, a single infected node, and all nodes infected.