Cheapest Fiber Plan Connecting Every Data Center

Last updated: November 29, 2026

Quick Overview

Given n data centers and a list of possible fiber links [a, b, cost], return the minimum total cost of a set of links that connects every data center, or -1 if no such set exists.

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

0

0

0 solved


Given n data centers and a list of possible fiber links [a, b, cost], return the minimum total cost of a set of links that connects every data center, or -1 if no such set exists.

A Meta onsite question grounded in backbone network planning between data center regions. The interviewer is looking for Kruskal's algorithm with union find, a correct stopping condition, and the ability to justify the greedy choice with the cut property.

What the Interviewer Expects
  • Sort candidate links by cost
  • Use union find to skip links that would form a cycle
  • Stop once n - 1 links are chosen
  • Return -1 when the graph is disconnected
  • Explain the cut property behind Kruskal
Key Topics to Cover
Kruskal
minimum spanning tree
union find
greedy
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 compare Kruskal and Prim for a dense graph?
  • How would you require at least two disjoint paths between every pair for redundancy?
  • Which links are in every minimum spanning tree, and which in none?
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 data centers 0..n-1 and links [a, b, cost], undirected.
  • Output: minimum total cost to connect all, or -1....
High-Level Design

This is a minimum spanning tree. Kruskal's algorithm: sort links by cost and add each link if it joins two different components. Union find answers "different components?" in near-constant time.

Why ...


Submit Your Answer
Markdown supported