Count the Number of Complete Components
Reported by candidates from Amazon's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
An isolated vertex counts as a complete component. That one line in the Amazon problem, reported in July 2026, trips people who rush the edge cases. The task is Count the Number of Complete Components: given n and an undirected edge list, count the connected components where every pair of vertices has a direct edge. It's a graph problem with a tidy counting check at the end. If you blank on the check mid-assessment, StealthCoder runs invisibly as a safety net and hands you the solution while the proctor sees nothing.
The problem
You are given an integer n and an undirected graph whose vertices are numbered from 0 through n - 1. The array edges contains each undirected edge [u, v].
A connected component is complete when every pair of distinct vertices in that component is joined by an edge.
Return the number of complete connected components. A component containing one vertex is complete.
Function
countCompleteComponents(n: int, edges: int[][]) → int
Examples
Example 1
n = 6
edges = [[0,1],[0,2],[1,2],[3,4]]
return = 3
The components are {0,1,2}, {3,4}, and {5}. Each contains every possible internal edge, so all three are complete.
Example 2
n = 6
edges = [[0,1],[0,2],[1,2],[3,4],[3,5]]
return = 1
The component {0,1,2} is complete. The component {3,4,5} is missing edge [4,5], so it is not complete.
Example 3
n = 1
edges = []
return = 1
The only vertex forms a one-vertex complete component.Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: a component with v vertices is complete exactly when it has v*(v-1)/2 edges. So you don't check every pair. Find each component with union-find or DFS, count its vertices, and count its edges. Compare edges to v*(v-1)/2. With union-find, tally vertices per root, then loop over edges and add one to the root's edge count. With DFS, sum the degrees of the component's vertices and divide by 2. The common pitfall is forgetting singletons. A lone vertex has 0 edges and 0*(−1)/2 is 0, so it passes the check and counts. Example 3 tests this. Another slip is counting edges twice in an undirected adjacency list. Divide the degree sum by 2. If the formula slips your mind during the live OA, StealthCoder is the hedge that reads the problem and gives you the working code.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Count the Number of Complete 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. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Amazon's OA.
Amazon reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Count the Number of Complete Components FAQ
What's the trick in Count the Number of Complete Components?+
Use the edge count formula. A component with v vertices is complete only if it has exactly v*(v-1)/2 edges. Find components with DFS or union-find, count vertices and edges in each, and compare. You never need to test every vertex pair.
How hard is this problem really?+
Medium on paper, but it's friendly. You need basic component finding plus one formula. Most of the difficulty is remembering the edge count check and handling single-vertex components. If you've written union-find or DFS before, it's quick.
Does a single isolated vertex count as complete?+
Yes. The statement says so, and Example 3 shows it with n = 1 and no edges. The formula agrees, since 1 vertex needs 0 edges. Every isolated node adds one to your answer, so don't skip components with no edges.
Should I use DFS or union-find here?+
Either works. DFS is easy if you build an adjacency list: sum degrees in a component, halve it, and compare to the formula. Union-find is clean if you already have a template: track size per root, then count edges per root. Pick the one you can write without bugs.
How do I prepare for this in 48 hours?+
Write it once with union-find and once with DFS, using the three given examples as tests. Focus on the edge formula and the singleton case. Also rehearse building an adjacency list from an edge list fast, since that setup is where time leaks.