Reported September 2026
Amazonunion find

Minimum Edge Moves to Connect a Network

Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Amazon OA. Under 2s to a working solution.
Founder's read

The edge case that wrecks a naive solution here is the cable count. Amazon reported this one in September 2026, and it looks like a plain connectivity question until a test with self-loops or parallel edges shows up. You get a network of n nodes and a list of cables. You can pull a cable and replug it anywhere. The job is the minimum number of moves to join everything, or -1 if it can't be done. It's a graph problem, and union-find or DFS solves it in a few lines. If you blank during the live OA, StealthCoder runs invisibly as a safety net.

The problem

You manage an undirected network with nodes 0 through n - 1. In one move, you may remove one existing cable and reconnect that cable between any two nodes.
Return the minimum number of cable moves needed to make every node connected. Return -1 when the network does not contain enough cables to connect all nodes.

Function
minimumEdgeMoves(n: int, edges: int[][]) → int

Examples
Example 1
n = 4
edges = [[0,1],[0,2],[1,2]]
return = 1
One cycle edge can be moved to attach the isolated node.
Example 2
n = 6
edges = [[0,1],[0,2],[0,3],[1,2]]
return = -1
Four cables cannot connect six nodes.

Constraints
1 <= n <= 100000.
Every edge has two valid node IDs.
Self-loops and parallel edges are allowed and still count as available cables.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you don't simulate any moves. Count connected components with union-find or DFS. You need components - 1 moves to link them all. First check feasibility: if edges.length < n - 1, return -1 immediately, because no rearrangement can connect n nodes with fewer cables. If you have enough cables, the answer is always components - 1, since redundant cables (cycles, self-loops, parallel edges) supply the spare ones. The common pitfall is trying to count redundant edges separately, or skipping self-loops and parallel edges when counting cables. They count as available cables, so use the raw length of edges. Another pitfall is recursive DFS at n = 100000, which can overflow the stack. Use iterative DFS or union-find with path compression. Complexity is near O(n + m). If the logic slips under pressure, StealthCoder can hand you the solution during the live assessment.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Minimum Edge Moves to Connect a Network cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of operations to make network connected. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Amazon's OA.

Amazon reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Edge Moves to Connect a Network FAQ

What's the trick in Minimum Edge Moves to Connect a Network?+

Check edges.length >= n - 1 first, else return -1. Then count connected components. The answer is components - 1. You never need to track which specific cable is redundant, because if the total count suffices, enough spare cables always exist.

Why do self-loops and parallel edges matter?+

They count as real cables. A self-loop connects nothing new, so it's always spare and movable. Parallel edges are the same. Use the raw edges length for the feasibility check, and don't filter or dedupe the input before counting.

Should I use union-find or DFS?+

Either works. Union-find with path compression is shorter and avoids recursion depth issues at n = 100000. Start with n components, decrement on each successful union, and return the final count minus one. Iterative DFS or BFS is fine too.

How hard is this really?+

Easy to medium. The code is short, but the insight that you only need a count, not a simulation, trips people up. If you've seen connected-components counting before, you can solve it in about 10 minutes.

How do I prepare in 48 hours for this kind of OA question?+

Write union-find from memory twice. Practice counting components on a few small graphs, including isolated nodes and cycles. Then test your code on the two examples plus n = 1 with no edges, which should return 0, and a case with only self-loops.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Amazon.

OA at Amazon?
Invisible during screen share
Get it