A reachable town is being reported as unreachable in this road-network codebase. Find and fix the bug.
EasyYou're debugging a mayor's road-network tool. It loads roads between towns from JSON, builds a graph, and answers whether one town can reach another. Residents report that some towns show as unreachable when there's clearly a road. Find the bug, fix it, then add the travel-time query the mayor asked for next.
What's in the codebase
road_network.py.load_roads,build_network,is_reachable, and a stubbedtravel_time. The docstrings are the spec. This is the only file you change.roads.json. Eight road records over eight towns. Every road is two-way in the real network, and each has a travel time in minutes.road_map.png. The same map as a diagram. Hill and Ivy form a separate component with no road to the rest.test_road_network.py. Fourteen tests covering loading, reachability in both directions, the isolated component, unknown towns, a start that equals the end, travel times, the zero-minute road, and termination on a map with cycles.scenarios.py. The mayor's checks. PrintsPASSwhen every check matches the contract.README.md. Run commands, the contract, and the map.
Out of the box, three tests pass, two reachability tests fail, and nine error because travel_time raises NotImplementedError. scenarios.py prints FAIL.
Your task
Make is_reachable correct for every road, then implement travel_time(network, start, end). Keep the function signatures.
travel_time returns the total minutes of any valid path, not necessarily the shortest. It returns 0 when start == end, None when no path exists, and raises ValueError for a town that isn't in the network. Elm to Fern is a real road of 0 minutes, so that query must return 0, not None. The map has cycles, so the search must terminate.
is_reachable(network, "Cedar", "Alder") -> True is_reachable(network, "Alder", "Hill") -> False travel_time(network, "Alder", "Elm") -> one of 25, 50, 70 travel_time(network, "Alder", "Cedar") -> one of 25, 70, 80, 95 travel_time(network, "Elm", "Fern") -> 0 travel_time(network, "Alder", "Hill") -> None travel_time(network, "Alder", "Alder") -> 0 travel_time(network, "Alder", "Nowhere") -> ValueError
You're done when all 14 tests pass and python3 scenarios.py prints PASS.
The shortest path follow-up
Once the tests pass, the mayor asks for the fastest route instead of any route. Talk through what changes in the contract, the algorithm, and the tests, and what the loader would have to reject that it tolerates today.
Using AI on this problem
This round is usually run at the entry level, often with AI limited to lookups. Use it to check standard library behavior, such as how json.load returns records. Don't hand it the failing test and ask for a fix: tracing what build_network stores for one road is the signal, and so is the check that keeps 0 and None apart. Read any generated search code against the zero-minute road before you trust it.
Why the starter is broken
build_network adds each road to the from town's list only. For the record from Alder to Birch, Alder gets Birch as a neighbor, but Birch gets an empty list entry and never learns about Alder. is_reachable is a correct search running on a one-way graph, so every query that needs a road in its reverse direction returns False. Cedar to Alder and Fern to Alder fail for this reason.
Approach
- Trace one record through
build_networkand print the adjacency for Birch. The reverse entry is missing. - Store every road in both directions, with the same minutes. Leave
is_reachablealone, because the search is right once the graph is. - Implement
travel_timeas a recursive depth-first search. The destination returns0, a dead end returnsNone, and a successful branch adds its road's minutes on the way back up. - Keep a visited set so the search terminates on cycles.
- Check the recursive result with
is not None, never with truthiness.
Pythondef build_network(records):
network = {}
for record in records:
start = record["from"]
end = record["to"]
minutes = record["minutes"]
network.setdefault(start, []).append((end, minutes))
network.setdefault(end, []).append((start, minutes))
return network
def travel_time(network, start, end):
_check_town(network, start)
_check_town(network, end)
visited = {start}
def dfs(town):
if town == end:
return 0
for neighbor, minutes in network[town]:
if neighbor in visited:
continue
visited.add(neighbor)
rest = dfs(neighbor)
if rest is not None:
return minutes + rest
return None
return dfs(start)
A single visited set shared across the whole search is enough here. When a branch fails, the search has already explored every town that branch could reach, and none of them was the destination, so there's no reason to explore them again. That keeps the search at O(V + E). Removing towns from the set on backtrack also passes the tests, but it explores every simple path in the worst case, which is exponential.
With the fix, travel_time(network, "Alder", "Elm") returns 70 through Birch, Cedar, and Dahlia, because the adjacency lists put Cedar before Elm. That's valid under the contract even though 25 exists. All 14 tests pass and scenarios.py prints PASS.
What to focus on
- Fix the data structure, not the search. A search that looks both ways would hide a graph that's still wrong for every other caller. One line in
build_networkfixes every road. - Treat 0 as an answer.
if rest:turns the 0-minute Elm to Fern road into a failed branch. The zero-minute test exists to catch exactly that. - Keep three outcomes apart. An unknown town raises
ValueError, an unreachable town givesFalseandNone, and the same town givesTrueand0. - Know why the visited set terminates. Without it, the Alder, Birch, Elm triangle loops forever. Be ready to say why one shared set doesn't lose valid answers for an any-path query.
The shortest path follow-up
The contract changes from any total to the minimum total, so the tests would assert one exact value per query instead of a set. Depth-first search returns whichever path it finds first, so it has to go. Because minutes are non-negative, Dijkstra's algorithm works: always expand the town with the smallest known total, and the first time the destination comes off the heap, its total is the minimum.
On this map the shortest times are 25 for Alder to Elm and Alder to Fern and 0 for Elm to Fern, and Alder to Hill still returns None. The zero-minute road is still fine, but a negative minutes value would break Dijkstra, so load_roads should reject negative records instead of passing them through. Duplicate roads are harmless, since the cheaper one wins.
Leveling signals
Interview experiences
1 sharedRelated courses


