Problem · Graph

Visible Profiles Count

Learn this problem
MediumMicrosoftINTERNOA
See Microsoft hiring insights

Problem statement

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 = 7connection_edges = 4connection_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.

More Microsoft problems

drafts saved locally
public int[] visibleProfilesCount(int connection_nodes, int connection_edges, int[] connection_from, int[] connection_to, int[] queries) {
  // write your code here
}
connection_nodes7
connection_edges4
connection_from[1,2,3,5]
connection_to[2,3,4,6]
queries[1,3,5,7]
expected[4,4,2,1]
checking account