Cheapest Fiber Build That Connects Every Data Center

Last updated: November 29, 2026

Quick Overview

Given n data centers and a list of candidate fiber routes (a, b, cost), choose a set of routes that connects every data center at the minimum total cost, returning that cost or -1 if no choice connects them all.

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

0

0

0 solved


Given n data centers and a list of candidate fiber routes (a, b, cost), choose a set of routes that connects every data center at the minimum total cost, returning that cost or -1 if no choice connects them all.

Google designs private backbone networks between its data centers, and minimum spanning tree questions come up framed this way in onsites. The interviewer checks that you pick Kruskal with union-find, can justify the greedy choice with the cut property, and detect a disconnected input.

What the Interviewer Expects
  • Chooses Kruskal and explains the cut property
  • Sorts routes by cost and uses union-find to reject cycles
  • Stops early after n - 1 accepted routes
  • Returns -1 when the graph is disconnected
  • Compares with Prim and says when each is better
Key Topics to Cover
Union-Find
Minimum Spanning Tree
Kruskal
Greedy
Sorting
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
  • Some routes are already built and must be included. How do you adapt?
  • Find the second-cheapest spanning build.
  • If each data center needs two disjoint paths for redundancy, is an MST still enough?
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 and a list of (a, b, cost) candidate routes, undirected, non-negative costs.
  • Output: minimum total cost of a connected spanning set, or -1 if impossible.
  • One data center needs nothi...
High-Level Design

Kruskal's algorithm: sort routes by cost, then scan, keeping a route only if it connects two currently separate groups. Union-find answers that in near-constant time. Correctness comes from the cut pr...


Submit Your Answer
Markdown supported