Reported May 2026
Amazonunion find

Count Connected Components

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 Amazon OA reported in May 2026 comes down to one data structure: union-find, also called disjoint set union. The task is simple on paper. Given n nodes and an edge list, return how many connected components exist. If you've seen it before, it's a five-minute job. If you haven't, it's easy to overbuild. Union-find or a plain DFS both work here. Pick one and write it clean. If your mind goes blank mid-assessment, StealthCoder runs invisibly on your screen and can hand you a working solution in real time, so one blank doesn't sink the whole OA.

The problem

You are given an undirected graph with n nodes and a list of edges. Each edge connects two nodes in the graph.
Return the number of connected components in the graph. A connected component is a maximal group of nodes where every pair of nodes is connected by some path.

Function
countConnectedComponents(n: int, edges: int[][]) → int
Complete the function countConnectedComponents in the editor.
countConnectedComponents has the following parameters:
int n: the number of nodes, labeled from 0 to n - 1
int edges[][]: an array where each element [u, v] represents an undirected edge between nodes u and v
Returns int: the number of connected components in the graph

Examples
Example 1
n = 5
edges = [[0, 1], [1, 2], [3, 4]]
return = 2
Nodes 0, 1, and 2 form one connected component. Nodes 3 and 4 form another connected component. Therefore, there are 2 connected components.
Example 2
n = 5
edges = [[0, 1], [1, 2], [2, 0], [3, 4]]
return = 2
The cycle among nodes 0, 1, and 2 is still one connected component. Nodes 3 and 4 form the second component.
Example 3
n = 4
edges = []
return = 4
With no edges, every node is isolated, so each node is its own connected component.

Constraints
Nodes are labeled from 0 to n - 1.
The graph is undirected.
The input edges are valid node pairs.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Start with n components, one per node. For each edge, find the roots of u and v. If the roots differ, merge them and subtract one from the count. If they match, the edge sits inside an existing component, so do nothing. That handles the cycle in Example 2 without special code. Return the count at the end. Use path compression and union by rank or size and you're near O(n + e) overall. The common pitfalls: forgetting isolated nodes, which is why you start the count at n and not at zero, and building an adjacency list that skips nodes with no edges. Recursive DFS can also blow the stack on a long chain, so go iterative if n looks large. Example 3 with no edges should return n. If you freeze live, StealthCoder is the safety net that reads the problem and gives you the union-find template.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Count Connected Components 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of connected components in an undirected graph. 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Connected Components FAQ

How hard is Count Connected Components really?+

It's a standard medium-easy graph problem. The logic is short, usually under 25 lines. The difficulty is recognizing that you only need component counts, not the paths. If you know union-find or DFS, you finish fast and spend the leftover time on edge cases.

What's the trick for this Amazon question?+

Start the count at n and subtract one every time an edge merges two different roots. Edges inside an existing component change nothing. That one rule covers cycles, duplicate edges, and isolated nodes without any extra branches.

Should I use DFS or union-find?+

Either passes. DFS needs an adjacency list and a visited array, and you count how many times you start a new search. Union-find skips the adjacency list and works straight off the edge list. Use whichever you can write without bugs under pressure.

What edge cases should I test before submitting?+

Test empty edges, which should return n. Test a single node. Test a cycle like Example 2. Test duplicate edges and a graph where everything is one component. Also check that isolated nodes with no edges still get counted.

How do I prepare in 48 hours for a graph OA like this?+

Write union-find from memory twice, with find, path compression, and union. Then write iterative DFS on an adjacency list. Run both on the three examples here. That covers most connectivity questions and takes about an hour total.

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