Cheapest Ocean Routing With a Transshipment Limit
Last updated: November 29, 2026
Quick Overview
Ports are nodes and priced lanes are directed edges. Find the cheapest way to move a container from an origin port to a destination port using at most K transshipments (intermediate ports), and return both the cost and the port sequence.
Flexport
November 29, 20260
0
0 solved
Ports are nodes and priced lanes are directed edges. Find the cheapest way to move a container from an origin port to a destination port using at most K transshipments (intermediate ports), and return both the cost and the port sequence.
Each transshipment adds handling cost, delay and damage risk, so Flexport-style routing caps how many a shipper will accept. The interviewer checks that you see why plain Dijkstra fails under a hop limit and that you can still reconstruct the chosen path.
What the Interviewer Expects
- Explain why unconstrained Dijkstra can return a route with too many hops
- Use K+1 rounds of Bellman-Ford relaxation reading only the previous round
- Reconstruct the actual route, not just the cost
- State complexity in ports, lanes and K
- Return a clear result when no route fits the limit
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the top three cheapest routes for a quote page?
- How would you combine cost and transit time into one objective, or show a Pareto set?
- What changes if lane prices depend on container type (20ft, 40ft, reefer)?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Ports are 0..n-1; lanes is a list of (u, v, cost) with non-negative costs. K transshipments means at most K+1 legs. Return (cost, path) or (None, []) if nothing fits....
High-Level Design
Dijkstra keeps only the single cheapest cost per port, which might use too many hops, and it would discard a slightly pricier but shorter prefix that is needed. Bellman-Ford limited to K+1 rounds fixe...