Cheapest Delivery Route With at Most K Hub Transfers

Last updated: November 29, 2026

Quick Overview

Distribution centers and hubs are connected by one-way freight lanes, each with a cost. Find the cheapest cost to move a shipment from a source to a destination passing through at most K intermediate hubs, or -1 if impossible.

Walmart Labs
Coding & Algorithms
Software Engineer
Walmart Labs
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Distribution centers and hubs are connected by one-way freight lanes, each with a cost. Find the cheapest cost to move a shipment from a source to a destination passing through at most K intermediate hubs, or -1 if impossible.

Walmart's supply chain moves freight through distribution centers and cross-dock hubs, and each extra transfer adds handling time and damage risk. The Walmart Labs onsite interviewer checks that you see why plain Dijkstra fails under a hop limit and that you bound Bellman-Ford correctly.

What the Interviewer Expects
  • Explain why unconstrained Dijkstra can return a route with too many hops
  • Translate K intermediate hubs into K + 1 edges
  • Run exactly K + 1 relaxation rounds
  • Relax from the previous round's copy to avoid chaining within a round
  • State O(K * E) complexity
Key Topics to Cover
shortest paths
Bellman-Ford
weighted graphs
hop constraints
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you return the actual route?
  • How would you adapt Dijkstra with a (node, hops) state, and when is it faster?
  • What if lanes have both cost and transit time and you minimize cost under a time limit?
  • How would you handle lanes that are closed on certain days?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: n nodes, a list of directed lanes (u, v, cost) with non-negative cost, a source, a destination and K. A route using t intermediate hubs uses t + 1 lanes. Output: the minimum total cost over r...

High-Level Design

Plain Dijkstra finds the globally cheapest route, which might use many hops, and it discards a pricier node label that would have used fewer hops. Bellman-Ford naturally counts edges: after round i, `...


Submit Your Answer
Markdown supported

Related Questions