Problem · Graph

Visible Profiles Count

Learn this problem
EasyIBM logoIBMFULLTIMEOA
See IBM hiring insights

Problem statement

A popular social media platform provides a feature to connect people online. Connections are represented as an undirected graph where a user can see the profiles of those they are connected to.

There are connection_nodes users numbered 1 to connection_nodes, and connection_edges connections where the pth pair connects nodes connection_from[p] and connection_to[p]. The queries array contains node numbers. Find the number of users whose profiles are visible to query[p]. Report an array of integers where the pth value is the answer to the pth query.

Function

visibleProfilesCount(connection_nodes: int, connection_edges: int, connection_from: int[], connection_to: int[], queries: int[]) → int[]

Complete the function visibleProfilesCount in the editor.

visibleProfilesCount has the following parameters:

  1. connection_nodes: an integer, the number of users
  2. connection_edges: an integer, the number of connections
  3. connection_from[p]: an array of integers, the starting nodes of each connection
  4. connection_to[p]: an array of integers, the ending nodes of each connection
  5. queries: an array of integers, the node numbers to query

Returns

int[]: an array of integers where the pth value is the number of visible profiles for the pth query

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]
Example 1 illustration

For each query, the number of visible profiles is calculated as follows:

  • Query 1: Visible Profiles are [1, 2, 3, 4]. Number of Visible Profiles is 4.
  • Query 3: Visible Profiles are [1, 2, 3, 4]. Number of Visible Profiles is 4.
  • Query 5: Visible Profiles are [5, 6]. Number of Visible Profiles is 2.
  • Query 7: Visible Profiles are []. Number of Visible Profiles is 1 (the user can see their own profile).

Therefore, the function returns [4, 4, 2, 1].

Constraints

🐳

More IBM 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