Problem
Viewable Profiles by User
Learn this problemProblem statement
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 = 7u = [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 <= nodesu.length == v.length1 <= u[i], v[i], queries[i] <= nodes
More Salesforce problems
- Minimize Total Input Cost (for LTMS)Seen Jun 2026
- Count Prime StringsONSITE INTERVIEW · Seen Jun 2026
- Final Pod Counts After LogsOA · Seen May 2026
- ATM Queue Exit OrderPHONE SCREEN · Seen May 2026
- Good Ways to Split an ArrayPHONE SCREEN · Seen May 2026
- Generate Seen Binary StringsOA · Seen May 2026
- Update Pod Counts From LogsOA · Seen May 2026
- Minimum Removals to Balance ArrayOA · Seen May 2026