Cheapest Route Between Data Centers Using at Most K Hops

Last updated: November 29, 2026

Quick Overview

Given directed transfer links (from, to, cost) between data centers, a source, a destination, and a limit k, return the cheapest total cost to move data from source to destination using at most k intermediate hops, or -1.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given directed transfer links (from, to, cost) between data centers, a source, a destination, and a limit k, return the cheapest total cost to move data from source to destination using at most k intermediate hops, or -1.

A constrained shortest-path problem in the style of Meta's onsite coding rounds, framed around routing a bulk backup between regions where each relay adds latency risk. The interviewer checks that you see plain Dijkstra breaks under a hop limit and that you can do a bounded Bellman-Ford or level-limited BFS correctly.

What the Interviewer Expects
  • Explains why standard Dijkstra can discard a path that is cheaper in hops
  • Uses k+1 rounds of Bellman-Ford relaxation
  • Copies the distance array each round so one round adds only one edge
  • Returns -1 when the destination stays unreachable
  • States O(k * E) complexity
Key Topics to Cover
Shortest path
Bellman-Ford
Graphs
Dynamic programming
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?
  • Could Dijkstra work if the state includes hops used?
  • What if costs can be negative?
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, links as (u, v, cost) with non-negative cost, src, dst, and k. Output: minimum cost with at most k stops between src and dst, that is at most k + 1 edges. n up to 100, link...

High-Level Design

Let dist_r[v] be the cheapest cost to reach v using at most r edges. Start with dist_0[src] = 0. Each round, copy the previous array and relax every edge from the previous round's values only: `ne...


Submit Your Answer
Markdown supported