Find the Redundant Fiber Link That Closes a Loop

Last updated: November 29, 2026

Quick Overview

A backbone of n routers labeled 1 to n was laid out as a tree, then one extra fiber link was added. Given the links in installation order, return the link that can be removed to restore a tree; if several qualify, return the one installed last.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

0

0 solved


A backbone of n routers labeled 1 to n was laid out as a tree, then one extra fiber link was added. Given the links in installation order, return the link that can be removed to restore a tree; if several qualify, return the one installed last.

Google network engineering cares about loops in physical topologies, and this is a gentle union-find screen built on that framing. The interviewer checks that you see the first edge joining two already-connected nodes is the answer, and that you can explain why that is also the last valid candidate in input order.

What the Interviewer Expects
  • Processes links in order with a disjoint set
  • Returns the first link whose endpoints already share a root
  • Explains why that link is the last one on the cycle in input order
  • Writes iterative find to avoid recursion depth issues
  • Handles inputs with no cycle gracefully
Key Topics to Cover
Union-Find
Cycle Detection
Graphs
Trees
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
  • What if the links are directed and the result must be a rooted tree?
  • How would you return every link on the cycle, not just one?
  • If links arrive as a stream with deletions, does union-find still work?
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: list of [a, b] links over routers 1..n, with exactly one extra link making a single cycle.
  • Output: one link to remove so the remaining graph is a spanning tree.
  • Tie rule: among the li...
High-Level Design

Add links one by one into a union-find structure. A link (a, b) closes a loop exactly when a and b are already in the same set. Because there is only one cycle, the first time this happens we ha...


Submit Your Answer
Markdown supported