Visible Profiles Count
Reported by candidates from Microsoft's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Microsoft reported this one in March 2026, and the input size is the whole point. You get a graph, a pile of queries, and a naive plan of running a search per query will crawl once nodes and queries get big. The problem is Visible Profiles Count: for each user, return how many profiles they can see, which is just the size of their connected component. It's a union-find or component-labeling problem dressed up as a social network. If you recognize that in the first minute, the rest is typing. If you blank, StealthCoder is the invisible safety net running during the live OA.
The problem
A social media platform represents user connections as an undirected graph. Each node is a user, and each edge is a connection between two users. If two users are directly or indirectly connected, they can view each other's profiles. A user can also view their own profile. The network contains connection_nodes users numbered from 1 to connection_nodes and connection_edges connections. The ith connection joins connection_from[i] and connection_to[i]. For each user ID in queries, return the total number of profiles accessible to that user. Function visibleProfilesCount(connection_nodes: int, connection_edges: int, connection_from: int[], connection_to: int[], queries: int[]) → int[] Examples Example 1 connection_nodes = 7 connection_edges = 4 connection_from = [1,2,3,5] connection_to = [2,3,4,6] queries = [1,3,5,7] return = [4,4,2,1] Users 1, 2, 3, and 4 form one connected component, so queries 1 and 3 each return 4. Users 5 and 6 form a component of size 2. User 7 is isolated but can view their own profile, so the final result is 1.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: visibility is transitive and symmetric, so everyone in a connected component sees the same count. Build the components once, then answer every query with a lookup. Use union-find with path compression and union by size, track the size at each root, and answer each query with size[find(q)]. Or run one BFS/DFS per unvisited node and store the component size for every member. Either way it's O(N + E + Q), not O(Q * (N + E)). Pitfalls: users are numbered from 1, so size your arrays at n+1. Isolated nodes must return 1, not 0. Recursive DFS can overflow the stack on a long chain, so go iterative or use union-find. Duplicate edges and self-loops are harmless with union-find. If the live OA has you freezing on the setup, StealthCoder can hand you the solution in real time without the proctor seeing it.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Visible Profiles Count 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Microsoft's OA.
Microsoft 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.
Visible Profiles Count FAQ
What's the trick in Visible Profiles Count?+
Reduce it to connected component sizes. Everyone in a component sees the same number of profiles, including themselves. Compute components once with union-find or DFS, store each component's size, then answer every query with a single lookup instead of searching again.
Why does brute force fail here?+
Running a BFS or DFS for every query costs O(N + E) each time. With many queries and a large graph, that multiplies into a timeout. Precomputing component sizes once makes each query O(1) or near it, which is what the input size is pushing you toward.
Should I use union-find or DFS?+
Either works. Union-find is shorter and avoids recursion depth problems, so it's the safer pick for a long chain of connections. Keep a size array at each root, merge by size, and use path compression. DFS is fine if you write it iteratively.
What edge cases break most solutions?+
Isolated users must return 1 because they can view their own profile. Node IDs start at 1, so off-by-one array sizing bites people. Duplicate edges and repeated queries should also work. Test with a node that never appears in any edge, like user 7 in the example.
How do I prepare for this in 48 hours?+
Write union-find from memory twice until it's automatic: find with path compression, union by size, size tracking. Then solve a couple of connected-components problems like Number of Provinces. This problem is a direct application, so pattern recognition matters more than volume.