As reported: Google phone screen (2025), interviewer from Romania: cities connected by one-way roads; given the street connections, find for each city the distance to the closest market (there can be more than one market). No examples were given, so the candidate made up examples to confirm the task. Dijkstra came to mind first; multi-source BFS on the reversed graph was the solution, since the roads are unweighted. Small follow-up: mark unreachable cities with
-1instead ofINT_MAX. The round was passed.
There are n cities 0 … n − 1 and one-way roads: [u, v] lets you drive from u to v. Some cities have a market. For every city, return the minimum number of roads needed to drive from it to any market, or -1 if no market can be reached. A city with a market has distance 0.
Example 1
n = 6, roads: 0→1 1→2 2→3 4→2 5→4, markets = [3, 5]
-> 3 2 1 0 2 0
Example 2
n = 4, roads: 0→1 1→0 2→3, markets = [0]
-> 0 1 -1 -1 from 2 the only road leads to 3, a dead end
Constraints: 1 ≤ n ≤ 10^5; 0 ≤ roads.length ≤ 2·10^5; 1 ≤ markets.length ≤ n. Roads may repeat or be loops.
Code runner format: the first line is n m k, then m lines u v, then a line with the k market cities. Implement distanceToMarket(n, roads, markets); the harness prints the distances space-separated.