MEDIUMasked at 3 companies

Number of Connected Components in an Undirected Graph

A medium-tier problem at 64% community acceptance, tagged with Depth-First Search, Breadth-First Search, Union Find. Reported in interviews at General Motors and 2 others.

Founder's read

Connected components are foundational to graph problems, and General Motors, X, and LinkedIn all ask this one. The trap is thinking you need a fancy algorithm when a simple DFS or BFS will solve it. You're given an undirected graph and need to count how many separate, isolated subgraphs exist. Most candidates know the pattern but stumble on edge cases: isolated nodes, empty graphs, or mishandling the visited set. The acceptance rate sits at 64%, which means roughly one in three candidates either overthink it or miss a subtle detail during the live assessment. StealthCoder runs invisibly if this hits your OA and you blank on the traversal order.

Companies asking
3
Difficulty
MEDIUM
Acceptance
64%

Companies that ask "Number of Connected Components in an Undirected Graph"

If this hits your live OA

Number of Connected Components in an Undirected Graph is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop.

Get StealthCoder
What this means

The core trick is realizing you need to iterate through every node and run a graph traversal (DFS or BFS) on unvisited ones, incrementing your component counter each time you start a fresh traversal. The gotcha most people hit: forgetting that isolated nodes count as components of size one. Your visited set must be global across all traversals, not reset per component. Union Find is the alternative approach, equally valid but slower for most interview contexts. The algorithm itself is O(n + m) where n is nodes and m is edges. What kills candidates under pressure is sloppy visited-set management or returning the count before the loop finishes. If you're stuck during your assessment and the pattern isn't clicking, StealthCoder surfaces a working DFS or BFS solution in seconds, invisible to the proctor.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Number of Connected Components in an Undirected Graph recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an Amazon engineer who used it to pass JPMorgan's OA and system design loop. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Number of Connected Components in an Undirected Graph interview FAQ

Is this problem still asked at FAANG-level companies?+

Yes. General Motors, X, and LinkedIn all report this. It's a core graph fundamentals problem, not trendy enough to rotate out. Expect it more often as a warm-up or early-stage screening question rather than a final-round curveball.

What's the difference between DFS and BFS for this problem?+

Both work and run in identical O(n + m) time. DFS uses a stack (implicit via recursion), BFS uses a queue. DFS is slightly cleaner to write under pressure. Choose whichever you code faster without mistakes.

Do isolated nodes count as connected components?+

Yes, absolutely. A node with no edges is a component by itself. This is where many candidates lose points. Make sure your traversal loop checks every node, not just edges.

Should I use Union Find or DFS for the live assessment?+

DFS or BFS. Union Find works but adds complexity and is slower in interviews unless the problem explicitly asks for dynamic connectivity. Keep it simple and code fast.

How does this relate to the other graph topics I need to know?+

Connected components is the foundation for cycle detection, bipartiteness checking, and topological sorting. Master this pattern first. It also pairs naturally with Depth-First Search and Breadth-First Search, both essential for any graph round.

Want the actual problem statement? View "Number of Connected Components in an Undirected Graph" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.