Find the Patch Cable That Creates a Loop in a Rack Network

Last updated: November 29, 2026

Quick Overview

A rack network started as a tree of n switches and then one extra patch cable was added, creating exactly one loop. Given the cables in the order they were installed, return the last-installed cable whose removal restores a tree.

Microsoft
Coding & Algorithms
Software Engineer
Microsoft
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


A rack network started as a tree of n switches and then one extra patch cable was added, creating exactly one loop. Given the cables in the order they were installed, return the last-installed cable whose removal restores a tree.

A union find question in the style of Microsoft technical screens, framed around detecting broadcast loops in an Azure rack. The interviewer checks that you union endpoints in order and return the first cable whose endpoints are already connected, and that you know path compression and union by size.

What the Interviewer Expects
  • Process cables in installation order
  • Use union find to test connectivity in near-constant time
  • Return the first cable joining two already-connected switches
  • Explain why that cable is the last one on the loop in input order
  • Implement path compression and union by size
Key Topics to Cover
union find
cycle detection
undirected graphs
path compression
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 graph is directed and one node could end up with two parents?
  • How would you report every cable on the loop?
  • How would you detect loops continuously as cables are added and removed?
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 n edges [u, v] over switches 1..n, forming a tree plus one extra edge. Output: the edge that, if removed, leaves a tree; if several qualify, the one appearing last in the input....

High-Level Design

Add cables one by one with a disjoint set union. Before linking u and v, check whether they already share a root. If they do, this cable closes the loop. Because every cable before it was accepted...


Submit Your Answer
Markdown supported