Skip to main content
All Questions

A reachable town is being reported as unreachable in this road-network codebase. Find and fix the bug.

Easy
Unlock detailed company stats for this questionUpgrade

You'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.

Starter code and solution
Python 3.11+ (standard library)
Download code

What's in the codebase

  • road_network.py. load_roads, build_network, is_reachable, and a stubbed travel_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. Prints PASS when 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.

Related courses

Course

Forward Deployed Engineering Interviews

Land your next role in forward deployed engineering (FDE) at top companies like Google, Anthropic, OpenAI, and startups. Learn from mock interviews, frameworks, and advice from senior candidates. Perfect your system design, coding, and behavioral interviews.

Course

Behavioral Interviews: A Practical Guide for Engineers

Get offers at the level you deserve. Prep for senior+ behavioral interviews at FAANG and Tier-1 companies. Made with staff and principal engineers from Meta, Netflix, Google, and more. Avoid downleveling, elevate past projects, and stop sounding like a robot.