Shortest Paths to Multiple Targets
Reported by candidates from Waymo's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Waymo reported this one in September 2026, and the input sizes tell you the whole story: n up to 2 * 10^5 and 3 * 10^5 edges. Running a search per target is dead on arrival. It's single-source shortest paths with non-negative weights, so one Dijkstra from the source answers every target at once. The OA wants you to see that and not overbuild. If your head goes blank on the heap setup or the overflow detail, StealthCoder is the invisible safety net running during the live assessment. Otherwise, read on and get the template down.
The problem
You are given a directed weighted graph with n nodes labeled 1 through n. Each edge is represented as [u, v, w], meaning there is an edge from u to v with non-negative weight w. Also given are a source node source and a list of target nodes targets. Compute the shortest distance from source to each target in order. If a target is unreachable, return -1 for that target. Function shortestPathsToTargets(n: int, edges: int[][], source: int, targets: int[]) → long[] Complete the function shortestPathsToTargets in the editor below. shortestPathsToTargets has the following parameters: int n: the number of nodes int[][] edges: directed edges [u, v, w] int source: the source node int[] targets: the targets to query Returns long[]: shortest distances to the targets in the same order. Examples Example 1 n = 5 edges = [[1, 2, 2], [1, 3, 5], [2, 3, 1], [2, 4, 2], [3, 5, 1], [4, 5, 2]] source = 1 targets = [3, 4, 5] return = [3, 4, 4] Running Dijkstra from node 1 gives distances 3, 4, and 4 to targets 3, 4, and 5. Example 2 n = 4 edges = [[1, 2, 5]] source = 1 targets = [2, 3] return = [5, -1] Node 2 is reachable with cost 5, while node 3 is unreachable. Constraints 1 <= n <= 2 * 10^5 0 <= edges.length <= 3 * 10^5 0 <= w <= 10^9 Multiple edges and self-loops are allowed.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Run Dijkstra once from source with a min-heap, then read off the distances for each target in order. Weights are non-negative, so Dijkstra is valid. Bellman-Ford is too slow at these sizes, and Floyd-Warshall is hopeless. Complexity is O((n + m) log n). The pitfalls are concrete. Distances can reach about 2 * 10^5 * 10^9, so use 64-bit integers, which is why the return type is long. Initialize distances to a large sentinel and convert unreachable ones to -1 at the end. Skip stale heap entries by checking popped distance against the stored one. Self-loops and multi-edges are harmless with an adjacency list. Zero weights are fine. Targets may repeat, so just index into the array. If you freeze on the live OA, StealthCoder can hand you a clean Dijkstra without the proctor seeing it.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Shortest Paths to Multiple Targets 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Waymo's OA.
Waymo reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Paths to Multiple Targets FAQ
What's the trick in the Waymo shortest paths to multiple targets problem?+
Don't search per target. Run Dijkstra a single time from the source, store distances for all nodes, then answer each target by lookup. That turns many queries into one O((n + m) log n) pass, which fits the constraints easily.
Why not use Bellman-Ford or BFS?+
BFS ignores weights, so it gives wrong answers on weighted edges. Bellman-Ford is O(n * m), far too slow at 2 * 10^5 nodes and 3 * 10^5 edges. Weights are non-negative here, which is exactly the condition that makes Dijkstra correct and fast.
What overflow or edge-case bugs should I watch for?+
Use 64-bit for distances, since path sums can exceed 32-bit range with weights up to 10^9. Use a sentinel like Long.MAX_VALUE and map it to -1 at the end. Don't add to the sentinel while relaxing. Handle source equal to a target, which returns 0.
Do self-loops and multiple edges break Dijkstra?+
No. Store every edge in the adjacency list and let relaxation pick the smallest. A self-loop never improves a distance since weights are non-negative. Parallel edges just get compared, and the cheaper one wins naturally.
How do I prep for this in 48 hours?+
Write Dijkstra with a priority queue from scratch twice, in your OA language. Practice the stale-entry skip and the unreachable-to-minus-one conversion. Test on a graph with a disconnected node and a zero-weight edge. That covers nearly everything this problem can throw at you.