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
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

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
Bellman-Ford
shortest paths
weighted graphs
constrained paths
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 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 Problems
Sample 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...


Submit Your Answer
Markdown supported