Shortest delivery route that avoids dangerous stops
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked in Google's onsite coding round, last asked Dec 2025. It tests graphs and shortest paths.
Delivery stops are labelled 0 to n - 1. Each route is a list of stops, and you can move between stops that sit next to each other in a route, in either direction; every move is one step. Some stops are dangerous. Find the best way from source to target, first avoiding dangerous stops entirely, then allowing them at a cost.
Part 1: avoid dangerous stops
-
min_safe_steps(n, routes, source, target, dangerous) -> int— the fewest steps fromsourcetotargetthat never visit a dangerous stop. Return-1ifsourceortargetis dangerous, or if no safe path exists.
min_safe_steps(5, [[0, 1, 2, 3], [0, 4, 3]], 0, 3, {4}) -> 3 # 0-4-3 is blocked, so 0-1-2-3
min_safe_steps(5, [[0, 1, 2], [2, 3, 4]], 0, 4, {2}) -> -1 # every path goes through 2
Part 2: fewest dangerous stops, then fewest steps
Now dangerous stops may be used. Among all paths, minimise the number of dangerous stops visited; among those, minimise steps. A dangerous stop counts each time the path starts on it or enters it, so a dangerous source or target counts.
-
best_route(n, routes, source, target, dangerous) -> [dangerous_visits, steps], or[-1, -1]whentargetis unreachable.
best_route(6, [[0, 1, 5], [0, 2, 3, 4, 5]], 0, 5, {1}) -> [0, 4] # the long safe path beats 0-1-5
best_route(6, [[0, 1, 5], [0, 2, 3, 5]], 0, 5, {1, 2}) -> [1, 2] # both paths hit one; 0-1-5 is shorter
best_route(5, [[0, 1, 2], [2, 3, 4]], 0, 4, {2}) -> [1, 4]
Constraints
-
1 <= n <= 10^5, up to 10^5 routes and 2 · 10^5 stops listed across all routes. - A route with fewer than two stops adds no edges. The same edge may appear in several routes.
Clarifying questions
- Are moves allowed in both directions along a route?
- Do
sourceandtargetcount as visits when they are dangerous? - In Part 2, should I return only the steps or both numbers?
- Can
sourceequaltarget?
What a strong answer covers
- Build the adjacency list from consecutive pairs only, not every pair in a route.
- Part 1: BFS that never enqueues a dangerous stop, O(n + E).
- Part 2: the cost is the pair
(dangerous, steps), compared lexicographically. Dijkstra on that pair, or 0-1 BFS-style layering on the dangerous count. Each edge adds(1 if the next stop is dangerous, 1). - Why a boolean
visitedbreaks Part 2: a stop reached first by a short dangerous path must be revisited when a safer path reaches it later. Keep the best pair per stop and relax. - Tests: source equals target, dangerous source, disconnected graph, duplicate edges.
Follow-ups
- Allow at most
kdangerous stops and minimise steps (state = stop and dangerous count so far). - Routes have travel times instead of unit steps.
- Return the path itself, not just its cost.
Where it's asked
- Google · Onsite coding · Last asked Dec 202545 min · Usually two rounds, mostly in person: whiteboard plus a Chromebook with Google Docs · No AI (a pilot code-comprehension round lets candidates use Gemini)
What it tests
graphs · shortest paths
No official answer yet. Write yours on the right and run it.
Nothing published yet.
Sign in to submit your code; every attempt is kept here.
