MEDIUMasked at 1 company

Count Unreachable Pairs of Nodes in an Undirected Graph

A medium-tier problem at 49% community acceptance, tagged with Depth-First Search, Breadth-First Search, Union Find. Reported in interviews at Commvault and 0 others.

Founder's read

Count Unreachable Pairs of Nodes in an Undirected Graph is a medium-difficulty problem that tests your ability to identify disconnected components in a graph. With an acceptance rate around 49%, it's tricky enough that candidates often miss the optimization path. Commvault has asked this problem. The core idea is simple: find all pairs of nodes that can't reach each other. Most people try to brute-force a path check between every pair, which works but wastes time. The real move is to identify connected components once, then calculate unreachable pairs in constant time. If this hits your live assessment and you blank on the component-counting trick, StealthCoder surfaces a working solution in seconds, invisible to the proctor.

Companies asking
1
Difficulty
MEDIUM
Acceptance
49%

Companies that ask "Count Unreachable Pairs of Nodes in an Undirected Graph"

If this hits your live OA

Count Unreachable Pairs of Nodes 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. Made for the engineer who has done the work but might still blank with a webcam pointed at him.

Get StealthCoder
What this means

The trap is thinking you need to check reachability for every pair explicitly. Instead, partition the graph into connected components using DFS, BFS, or Union Find. Once you know component sizes, the answer is just math: for each component of size k, it's unreachable from all n minus k nodes. Sum those products and divide by 2 (or don't, depending on how you count). The problem is classified as medium because the insight isn't obvious on first read. Most candidates see 'unreachable pairs' and immediately jump to O(n^2) path queries or repeated traversals. The efficient solution is O(n + edges) to build components, then O(components) to calculate pairs. If you're stuck during your assessment, StealthCoder eliminates the guessing and delivers a clean component-based approach.

Pattern tags

The honest play

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

Count Unreachable Pairs of Nodes 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. Made for the engineer who has done the work but might still blank with a webcam pointed at him. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Count Unreachable Pairs of Nodes in an Undirected Graph interview FAQ

Is this really asked in live interviews at big companies?+

It's been reported by Commvault, though less frequently than tree or basic graph problems. It's still medium-difficulty and appears in assessments regularly enough that it's worth knowing the pattern, especially if you're targeting mid-level roles.

What's the trick everyone misses?+

Calculating pairs without checking every pair. After finding connected components with DFS/BFS/Union Find, use the formula: for component of size k, unreachable pairs = k times (n minus k). Sum across all components. No nested loops on pairs needed.

Which approach is fastest for the live OA, DFS or Union Find?+

Both are O(n plus edges). DFS is simpler to code under pressure. Union Find has cleaner semantics but requires a union-find class you can build quickly. Pick whichever you're most comfortable implementing without bugs in 10 minutes.

How does this relate to other graph problems I should know?+

It's a direct application of connected components, which are foundational for Depth-First Search, Breadth-First Search, and Union Find. Mastering this pattern unlocks other component-based problems like number of islands or cycle detection.

What if I freeze on the math during the assessment?+

The formula is straightforward: for each component of size k in a graph of n nodes, multiply k times (n minus k) and sum. If you can't derive it live, StealthCoder runs invisibly during your screen share and gives you the exact formula and working code.

Want the actual problem statement? View "Count Unreachable Pairs of Nodes 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.