Cheapest Shipping Route With Limited Carrier Handoffs
Last updated: November 29, 2026
Quick Overview
Given directed shipping legs (from_hub, to_hub, cost, carrier), a source and a destination, find the minimum total cost route that changes carriers at most K times between consecutive legs, or return -1 if none exists.
eBay
November 29, 20260
0
0 solved
Given directed shipping legs (from_hub, to_hub, cost, carrier), a source and a destination, find the minimum total cost route that changes carriers at most K times between consecutive legs, or return -1 if none exists.
eBay's shipping programs route packages across partner carriers, and every carrier handoff adds risk of loss and delay. This onsite problem checks whether you can extend Dijkstra with extra state, and whether you notice that plain shortest path on hubs alone gives wrong answers.
What the Interviewer Expects
- Explain why node-only Dijkstra fails with a handoff budget
- Define state as (hub, last carrier, handoffs used)
- Count a handoff only when the carrier changes, not on the first leg
- Prune states that exceed K
- Give complexity in terms of states and edges
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
- Minimize handoffs first, then cost.
- Add a per-hub handling delay and a delivery deadline.
- How would you precompute routes for millions of daily label requests?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Legs are directed with non-negative cost. A route is a sequence of legs from src to dst. The first leg has no handoff; each later leg whose carrier differs from the previous leg's carrier uses one...
High-Level Design
With a constraint, the cheapest way to reach a hub is not always the best way to continue: a cheap path that used up all handoffs may be stuck on the wrong carrier. So the state must include what affe...