Viewable Profiles by User
Reported by candidates from Salesforce's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Strip away the social media story and the Salesforce problem reported in August 2025 is just this: how big is the connected component each queried user sits in? That's it. You get an undirected graph, a pile of queries, and one number per query. If you have this OA coming, expect a connected components problem in disguise, and a clean answer is short. StealthCoder is there as a safety net if your mind goes blank during the live assessment, but the idea here is simple enough to hold in your head tonight.
The problem
A social media platform represents user connections as an undirected graph. Users are numbered from 1 to nodes. If one user is directly or indirectly connected to another user, they can view that user's profile. You are given two arrays u and v, where u[i] and v[i] are connected. You are also given an array queries. For each queried user, return the number of profiles that user can view, including their own profile. Function viewableProfiles(nodes: int, u: int[], v: int[], queries: int[]) → int[] Examples Example 1 nodes = 7 u = [2,1,4,5] v = [1,3,5,6] queries = [1,5,7] return = [3,3,1] Users 1, 2, and 3 are connected, so user 1 can view 3 profiles. Users 4, 5, and 6 form another component. User 7 is isolated and can only view their own profile. Constraints 1 <= nodes u.length == v.length 1 <= u[i], v[i], queries[i] <= nodes
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is to stop treating each query as its own graph search. Build the components once, then answer every query with a lookup. Union-Find works well: union each pair (u[i], v[i]), track the size of each root, then for each query return the size at find(query). A DFS or BFS that labels components and records their sizes works just as well. Pitfalls: nodes are 1-indexed, so size your arrays at nodes+1. Isolated users never appear in u or v, so their answer must default to 1. Don't rerun a traversal per query, or large inputs will time out. Recursive DFS can also blow the stack on a long chain, so use iterative DFS or Union-Find with path compression. If you freeze mid-assessment, StealthCoder can surface the component-size approach in real time, but you should be able to write this unaided.
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 Viewable Profiles by User 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 Salesforce's OA.
Salesforce 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.
Viewable Profiles by User FAQ
What's the trick in the Salesforce Viewable Profiles problem?+
It's connected components. Group users who are directly or indirectly linked, count each group's size once, and answer every query by looking up the size of the group that user belongs to. The answer always includes the user, so the minimum is 1.
Should I use Union-Find or DFS for this?+
Either passes. Union-Find is shorter to write and avoids recursion depth issues. Keep a size array, union each edge, then return size[find(q)]. DFS or BFS is fine too if you label each component once and store its size, instead of searching per query.
How hard is this one really?+
Easy to medium. The graph story hides a standard pattern. If you've seen number of provinces or connected components before, you'll recognize it quickly. The risk is sloppy details like off-by-one indexing or forgetting isolated nodes, not the algorithm.
What edge cases break most solutions?+
Users who appear in no edge, which should return 1. Duplicate edges or self-loops, which shouldn't inflate sizes. 1-indexed users with arrays sized too small. Repeated queries for the same user, which is why you precompute sizes instead of searching again.
How do I prepare for this in 48 hours?+
Write Union-Find from memory twice, with path compression and union by size. Then solve one connected-components-size problem using iterative DFS. Practice returning answers for a query list by lookup. That covers this pattern, and it's still common in graph-flavored assessments.