Problem

Viewable Profiles by User

Learn this problem
SalesforceFULLTIMEOA
See Salesforce hiring insights

Problem 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 <= nodes
  • u.length == v.length
  • 1 <= u[i], v[i], queries[i] <= nodes

More Salesforce problems

drafts saved locally
public int[] viewableProfiles(int nodes, int[] u, int[] v, int[] queries) {
  // write your code here
}
nodes7
u[2,1,4,5]
v[1,3,5,6]
queries[1,5,7]
expected[3,3,1]
checking account