Cheapest Shipment Route With At Most K Hub Transfers
Last updated: November 29, 2026
Quick Overview
Given directed linehaul lanes between hubs with a cost per lane, find the cheapest cost to move a shipment from source hub to destination hub using at most K intermediate hubs, or -1 if impossible.
Flipkart
November 29, 20260
0
0 solved
Given directed linehaul lanes between hubs with a cost per lane, find the cheapest cost to move a shipment from source hub to destination hub using at most K intermediate hubs, or -1 if impossible.
An Ekart flavored weighted graph question in Flipkart onsites: more transfers mean more handling and damage risk, so planners cap them. The interviewer checks whether you notice plain Dijkstra is wrong under a hop limit and choose bounded Bellman-Ford or Dijkstra over (node, hops) state.
What the Interviewer Expects
- Explain why standard Dijkstra can discard a costlier path that uses fewer hops
- Use K + 1 rounds of Bellman-Ford relaxation with a copy of the previous round
- Return -1 when the destination is unreachable within the limit
- State O(K * E) time and O(V) space
- Mention the Dijkstra on (node, stops) alternative
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 actual route, not just the cost?
- What if each hub also adds a fixed handling fee?
- How would you precompute routes for every pair of hubs nightly?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
nhubs, directed lanes(u, v, cost)with non negative costs.- At most
Kintermediate hubs, so at mostK + 1lanes. - Output: min cost or -1.
- Hub count in the low thousands, lanes in the t...
High-Level Design
Plain Dijkstra keeps only the cheapest cost per hub. But a cheap path with many hops can block a slightly more expensive path with fewer hops that is the only one within the limit. Bellman-Ford natura...