Shipping route cost paths
I was asked this
No reports yet. Sign in to say where you were asked it.
A coding interview question asked at Stripe. It tests graphs, dfs and dijkstra.
A parcel carrier publishes its shipping routes as a list of strings. Each string has the form source:target:method:cost:
-
sourceandtargetare country codes. A route ships one way only. -
methodis the carrier or mode (for exampleAir,Sea,Rail). The same pair of countries can have several routes with different methods. -
costis a non-negative integer.
Implement a class ShippingNetwork(routes: list[str]) that answers these questions in parts:
-
direct_cost(src, dst) -> int | None. The cheapest single route fromsrctodst, orNoneif there is none. -
all_paths(src, dst, max_legs=2) -> list[Path]. Every way to ship fromsrctodstusing at mostmax_legsroutes (so by default, direct or through one intermediate country). A path must not visit a country twice. Each path lists its legs (source, target, method, cost) and its total cost. Return the paths sorted by total cost, cheapest first. -
cheapest_path(src, dst) -> Path | None. The cheapest path with any number of stops, orNoneifdstcannot be reached.
Reject malformed route strings with an error.
Example
routes = ["JP:DE:Air:9", "JP:SG:Sea:2", "SG:DE:Air:4", "JP:DE:Sea:7"]
-
direct_cost("JP", "DE")returns7. -
all_paths("JP", "DE")returns three paths:JP -Sea-> SG -Air-> DE(6),JP -Sea-> DE(7),JP -Air-> DE(9). -
direct_cost("DE", "JP")returnsNone, because routes are one way.
Example with more stops
routes = ["A:B:x:10", "A:C:y:1", "C:D:z:1", "D:B:w:1"]
-
all_paths("A", "B")returns only the direct path (10). The cheaper path needs two stops. -
cheapest_path("A", "B")returnsA -> C -> D -> Bwith cost 3.
Talk through how your design would change if the interviewer adds a limit on the number of stops, or bans some methods or countries.
Clarifying questions
- Do several routes between the same countries count as separate paths in
all_paths? - How are ties in total cost ordered in
all_paths? - Is a route from a country to itself possible, and should it be rejected?
- What should
cheapest_pathreturn whensrc == dst?
What a strong answer covers
- Parses each string with exactly four parts and a non-negative integer cost, rejecting anything else, and stores an adjacency map
source -> [(target, method, cost)]. -
direct_costtakes the minimum over the routes fromsrctodst. -
all_pathsis a depth-limited DFS that tracks visited countries and keeps each method as its own leg, then sorts by total cost. -
cheapest_pathis Dijkstra with a min-heap and parent pointers to rebuild the legs; valid because costs are non-negative.O(E log V). - Tests both examples, unreachable countries, one-way routes, and a cycle in the graph.
Follow-ups
- Limit the number of stops in
cheapest_path. - Ban some methods or countries.
- Find the cheapest path that uses only one method.
Where it's asked
What it tests
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
Sign in to submit your code; every attempt is kept here.
