Shortest Path with Mandatory Waypoint
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google asked this in April 2026: find the shortest path from source to destination, but you must pass through a mandatory waypoint. You're not just running Dijkstra once. You need to route through a specific node, which means computing shortest paths in stages. The trick is realizing you run the algorithm multiple times from different starting points, then stitch the results together. If you blank on the structure during the live OA, StealthCoder will feed you the pattern in real time so you don't lose points on the routing logic.
The problem
You are given a weighted graph with n nodes labeled 0 through n - 1. Each edge is represented as [u, v, w], meaning there is an edge between nodes u and v with non-negative distance w. Compute two values: If a required path does not exist, use -1 for that entry. Function Description Complete the function shortestPathWithWaypoint in the editor below. shortestPathWithWaypoint has the following parameters: Returns The shortest path from 0 to 4 is 0 -> 1 -> 2 -> 4 with total cost 6. That path already passes through the mandatory waypoint 1, so both answers are 6. The unconstrained shortest path is 0 -> 1 -> 3 with cost 2. There is no path from 2 to 3, so no valid route can pass through the waypoint.
Reported by candidates. Source: FastPrep
Pattern and pitfall
This is a shortest-path problem with a constraint. The standard move is to compute three separate shortest-path queries: source to waypoint, waypoint to destination, and source to destination unconstrained. Use Dijkstra or Bellman-Ford depending on the graph size and whether negative weights exist (the problem says non-negative, so Dijkstra is safe). The constraint forces a specific order: you must visit the waypoint before reaching the destination. The pitfall is trying to modify a single Dijkstra run instead of decomposing it. During the OA, if the graph structure or constraint wording trips you up, StealthCoder will show you the exact decomposition and which nodes to query from. The code itself is straightforward once you see the three-query shape.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Shortest Path with Mandatory Waypoint 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 by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as network delay time. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Path with Mandatory Waypoint FAQ
Do I really need to run Dijkstra three times?+
Yes. Query 1: source to waypoint. Query 2: waypoint to destination. Query 3: source to destination (unconstrained). Then combine the first two for the constrained answer. It's clean and avoids state explosion.
What if there's no path to the waypoint or from it?+
Return -1 for that case. The problem statement says so. Check both stages: if source-to-waypoint is -1 or waypoint-to-destination is -1, the constrained path is -1. The unconstrained path is independent.
Is this actually a graph problem or a DP problem?+
It's graph. You're computing shortest paths on a weighted graph. No memoization or state recurrence needed. Dijkstra or single-source shortest path is the core. The 'waypoint' is just a routing constraint, not a DP subproblem.
How do I handle the graph input format?+
Build an adjacency list or matrix from the edge list [u, v, w]. Directedness depends on the problem statement (check the examples). Most graph OAs give you edges and expect you to construct the representation. Use a dict of lists or a 2D array.
Will this timeout on large graphs?+
Dijkstra with a heap is O((V + E) log V). Three runs make it 3x that. If n is under 5000 and E is reasonable, you're fine. If the graph is huge, the OA would hint at a different approach. Standard Dijkstra is the expected answer here.