Cheapest Cross-Region Data Transfer With at Most K Intermediate Hops
Last updated: November 29, 2026
Quick Overview
Given directed transfer links between n Azure regions with egress prices, find the cheapest way to move data from region src to region dst using at most k intermediate regions. Return -1 if no such route exists.
Microsoft
November 29, 20260
0
0 solved
Given directed transfer links between n Azure regions with egress prices, find the cheapest way to move data from region src to region dst using at most k intermediate regions. Return -1 if no such route exists.
Asked in Microsoft onsites to test weighted shortest paths with an extra constraint. The interviewer wants to see why plain Dijkstra is wrong once hop count matters, and a bounded Bellman-Ford that copies the distance array each round.
What the Interviewer Expects
- Translate k intermediate stops into k + 1 edges
- Use k + 1 rounds of Bellman-Ford relaxation
- Relax from a snapshot of the previous round to avoid chaining edges within one round
- Explain why vanilla Dijkstra can discard the right answer
- State O(k * E) time
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 adapt Dijkstra with (cost, node, hops) states?
- What if prices can be negative credits?
- How would you return the actual route?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Inputs: n regions, directed links
[u, v, price], src, dst, and k. - A route may pass through at most k other regions, so it uses at most k + 1 links.
- Output: the minimum total price, or -1....
High-Level Design
Round i of Bellman-Ford computes the cheapest cost to every region using at most i links, provided each round relaxes only from the previous round's values. If we updated in place, one round could cha...