Reported July 2026
Visadepth first search

Connected Groups

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

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

The mistake that sinks a first attempt on this Visa OA, reported July 2026, is counting direct links instead of whole groups. Connected Groups hands you an N by N symmetric binary matrix and asks how many friend circles exist when relationships chain together. It's graph connectivity wearing a matrix costume. Members 1 and 3 in the second example never touch directly, yet they share a group through member 2. If you blank on the traversal, StealthCoder is the safety net running invisibly during the live OA, but the idea is small enough to own tonight.

The problem

Given an N by N symmetric binary matrix related, treat each index as one member. When related[i][j] == 1, members i and j are directly related.
Membership is transitive: members connected through any chain of direct relationships belong to the same group. Every member belongs to exactly one group, including an isolated member.
Return the number of connected groups.

Function
countConnectedGroups(related: int[][]) → int

Examples
Example 1
related = [[1,1,0],[1,1,0],[0,0,1]]
return = 2
Members 0 and 1 form one group, while member 2 is isolated and forms the second group.
Example 2
related = [[1,0,0,0],[0,1,1,0],[0,1,1,1],[0,0,1,1]]
return = 2
Member 0 is one group. Members 1, 2, and 3 form the other because 1 reaches 3 through 2.

Reported by candidates. Source: FastPrep

Pattern and pitfall

Treat the matrix as an adjacency matrix. Keep a visited array of size N. Loop i from 0 to N-1. If i isn't visited, increment the group count and run DFS or BFS from i, marking every index j where related[i][j] == 1 and j isn't visited. Each full traversal swallows one entire group. Union-Find works too: union i and j whenever the cell is 1, then count the roots. The classic pitfall is counting rows with a 1 off the diagonal, or counting pairs, which ignores transitivity. Another is forgetting that an isolated member still counts as a group, since the diagonal is 1 anyway. Complexity is O(N^2) time and O(N) space. Deep recursion can hit limits on large N, so an iterative stack is safer. If you freeze mid-assessment, StealthCoder can surface this exact traversal as a hedge.

If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.

If this hits your live OA

You can drill Connected Groups 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 passed his OA cold and still thinks the filter is broken.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as number of provinces. If you have time before the OA, drill that.

⏵ The honest play

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

Visa reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Connected Groups FAQ

What's the trick in Connected Groups?+

Count connected components, not edges. Walk each index, and when you find an unvisited one, start a DFS or BFS across the row's 1s, marking everything reachable. Each new start is one group. Transitivity is handled automatically by the traversal.

How hard is this Visa OA question really?+

Easy to medium. It's a standard connected components problem with the graph given as a matrix. If you've seen number of provinces, you've seen this. The difficulty is staying calm and not overcomplicating it with pair counting.

Should I use DFS, BFS, or Union-Find?+

Any works. DFS is shortest to write. BFS with a queue avoids recursion depth problems. Union-Find is clean if you like counting roots. All run in about O(N^2) because you scan the whole matrix once.

What edge cases should I test?+

Test a single member, a fully isolated matrix like the identity, and a fully connected matrix that should return 1. Also test a chain where the endpoints aren't directly related, like example 2, to confirm transitivity works.

How do I prepare for this in 48 hours?+

Write the DFS version from scratch twice, then the Union-Find version once. Trace both examples by hand. Focus on the visited array and the outer loop. That's the entire pattern, so you don't need more volume than that.

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

OA at Visa?
Invisible during screen share
Get it