Katkat

Count Connected Friend Groups

OpenAISoftware Engineer · Technical Screen

Last updated

Determine the number of connected components in an undirected graph represented as an adjacency matrix using graph traversal or disjoint set union.

Problem Statement

You are given an n x n binary matrix is_connected, where n represents the total number of people in a social network. The people are indexed from 0 to n - 1.

is_connected[i][j] = 1 if person i and person j are direct friends, and 0 otherwise. Friendship is mutually symmetric, meaning if person i is friends with person j, then person j is also friends with person i. Friendship is also transitive: if person A is friends with person B, and person B is friends with person C, then A, B, and C all belong to the same friend group.

Return the total number of distinct friend groups in the network.

Your function must treat the input matrix as read-only and avoid mutating it.

Function Signature

def count_friend_groups(is_connected: list[list[int]]) -> int:
    pass

Input & Output Format

  • Input: is_connected: A list of lists of integers representing an n x n symmetric adjacency matrix.
  • Output: An integer representing the count of connected components.

Constraints

  • 1 <= n <= 200, where n == len(is_connected) == len(is_connected[i])
  • is_connected[i][j] is either 0 or 1
  • is_connected[i][i] == 1 for all 0 <= i < n
  • is_connected[i][j] == is_connected[j][i] for all 0 <= i, j < n

Examples

Example 1

Input:
is_connected = [
  [1, 1, 0],
  [1, 1, 0],
  [0, 0, 1]
]
Output: 2
Explanation: 
Person 0 and Person 1 are friends, forming group 1.
Person 2 has no other friends and forms group 2 on their own.
Total groups = 2.

Example 2

Input:
is_connected = [
  [1, 0, 0, 1],
  [0, 1, 1, 0],
  [0, 1, 1, 0],
  [1, 0, 0, 1]
]
Output: 2
Explanation:
Person 0 and Person 3 are friends (group 1).
Person 1 and Person 2 are friends (group 2).
Total groups = 2.

Edge Cases to Consider

  • n = 1: A single isolated person should return 1.
  • Completely disconnected network (is_connected[i][j] == 0 for all i != j): Should return n.
  • Fully connected network (all entries are 1): Should return 1.

Hints

Hint 1

Model the problem as finding the number of connected components in an undirected graph where nodes are individuals and 1s represent edges.

Hint 2

A boolean visited array of size n can track which individuals have already been assigned to a group during a DFS or BFS traversal.

Hint 3

Iterate through each person from 0 to n - 1; whenever you encounter an unvisited person, increment your group counter and explore all transitively reachable people.

Follow-up questions

  • How would you adapt your solution if friendships are streamed dynamically as edge updates (e.g., person A and person B become friends)?
  • If n scaled to 1,000,000 but the graph was extremely sparse, how would you change the input representation and the algorithm's time and space complexity?
  • How would you modify the function to return the membership lists or sizes of all groups rather than just the count?