One Cable Too Many
An office network has n devices, numbered 0 to n - 1. It was wired as a tree, which means every device can reach every other device and there are no loops. Then someone plugged in one extra cable, and now packets can circle around a loop.
The installer's log lists every cable in the order it went in. You must unplug one cable so that the network is a tree again. Any cable on the loop would work, and the installer wants the one that was plugged in latest.
Your task
Write cableToUnplug(n, cables). cables has exactly n entries, each [a, b] joining devices a and b. It is a tree on all n devices plus one extra cable.
Return the cable whose removal leaves a tree. If several cables qualify, return the one that appears last in cables. Return it exactly as written in the list.
cableToUnplug(n: int, cables: int[][]) → int[]
Examples
n = 4 cables = [[0, 1], [1, 2], [2, 0], [2, 3]]
[2, 0]
The loop is 0-1-2-0, and unplugging any of its three cables leaves a tree. [2, 0] is the latest of them. The cable [2, 3] comes later in the log but is not on the loop, and unplugging it would cut device 3 off.
n = 5 cables = [[0, 1], [1, 2], [3, 4], [2, 3], [4, 1]]
[4, 1]
The loop is 1-2-3-4-1, made of [1, 2], [3, 4], [2, 3] and [4, 1]. The latest of those is [4, 1].
Constraints
- 3 ≤ n ≤ 1,000
- cables.length = n
- 0 ≤ a, b < n, a ≠ b, and no two cables join the same pair of devices
- Removing some cable leaves every device connected with no loop
Start with brute force
Try removing each cable, starting from the end of the log, and check with a depth-first search whether the rest is still connected. Return the first one that works.
Each check walks the whole network, so this takes O(n²) time, around a million steps for 1,000 devices. That is acceptable for these limits, but it repeats the same connectivity work for every cable, while union-find needs only one pass.
Hints
0 of 4 shown- Hint 1
Plug in the first example's cables one at a time, in log order. When does a loop first appear?
- Hint 2
A cable creates a loop exactly when its two devices are already connected by earlier cables. The loop appears when its last cable goes in, so that cable is the answer.
- Hint 3
So you need to track which devices are connected as cables arrive, and answer "are these two connected?" quickly. Which structure is built for that?
- Hint 4
Use union-find. Go through the cables in order. For each
[a, b], find both leaders. If they match,aandbwere already connected: return this cable. Otherwise merge the two groups.
Walkthrough and solution
Unlocks when you pass every test. Stuck for a while? Looking is fine too, as long as you then close it and write the code yourself.
How to solve it
Only cables on the one loop can be removed. Replay the log: until the loop closes, every cable joins two devices that were not yet connected. The cable that closes it is the first whose ends are already connected, and it is also the latest loop cable in the log. That is the answer.
Trace the second example, [[0,1],[1,2],[3,4],[2,3],[4,1]]:
- The cable [0, 1] joins two different groups, so they merge. The groups are now {0,1}, {2}, {3} and {4}.
- The cable [1, 2] joins different groups, so they merge. The groups are now {0,1,2}, {3} and {4}.
- The cable [3, 4] joins different groups, so they merge. The groups are now {0,1,2} and {3,4}.
- The cable [2, 3] joins different groups, so they merge into one group, {0,1,2,3,4}.
- The cable [4, 1] joins two devices that are already in the same group, so the function returns [4, 1].
A common slip is returning the last cable in the log. That works only when the loop closes at the end. In the first example the last cable, [2, 3], is a bridge to device 3.
function cableToUnplug(n, cables) {
const parent = Array.from({ length: n }, (_, i) => i);
function find(x) {
while (parent[x] !== x) {
parent[x] = parent[parent[x]];
x = parent[x];
}
return x;
}
for (const cable of cables) {
const ra = find(cable[0]), rb = find(cable[1]);
if (ra === rb) return cable; // already connected: this closes the loop
parent[ra] = rb;
}
return []; // never reached for valid input
}def cable_to_unplug(n, cables):
parent = list(range(n))
def find(x):
while parent[x] != x:
parent[x] = parent[parent[x]]
x = parent[x]
return x
for cable in cables:
ra, rb = find(cable[0]), find(cable[1])
if ra == rb:
return cable # already connected: this closes the loop
parent[ra] = rb
return [] # never reached for valid inputRun your code to see results here. Run checks the examples. Submit checks every test, including hidden edge cases.