This question evaluates understanding of graph traversal and shortest-path techniques under constraints, including avoidance of forbidden nodes, multi-criteria optimization (minimizing dangerous-node visits then steps), and reasoning about augmented state and visited-structure design.
You are given delivery stops (nodes) and a set of routes. Each route is a list of stops, and you can travel between adjacent stops in the same route (treat travel as an unweighted edge).
N
: number of stops labeled
0..N-1
.
routes
: a list of routes, where each route is a list like
[a, b, c, ...]
meaning edges
(a,b)
,
(b,c)
, ...
source
: starting stop.
target
: destination stop.
dangerous
: a set of stops considered dangerous.
Find the minimum number of steps from source to target without visiting any dangerous stop.
source
or
target
is dangerous, treat that as invalid (return
-1
).
-1
if no such path exists.
Now you may pass through dangerous stops.
Among all paths from source to target:
source/target
count if they are dangerous).
Return the minimum steps for the best path under the above ordering (or return both metrics if preferred—clarify with interviewer).
Explain how you would change your visited structure to correctly handle Part 2 (e.g., when the same stop can be reached with fewer dangerous stops or fewer steps).