Group employees connected through shared interests

Quick Overview

Given employee IDs and a list of interests for each employee, return the groups of employees connected directly or through a chain of shared interests. It tests modeling the input as a graph, finding connected components efficiently when one interest is shared by thousands of people, and returning a deterministic sorted result.

Group employees connected through shared interests

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

You are given `n` employees. The `i`-th employee has a unique ID `ids[i]` and a list of interests `interests[i]`. Two employees are directly connected if they share at least one common interest. Connections are transitive: if employee A is connected to B, and B is connected to C, then A, B and C all belong to the same group. Return all groups of employees, where each group is a list of employee IDs. An employee who shares no interest with anyone else forms a group by themselves. ### Function Signature ```python def group_employees(ids: list[int], interests: list[list[str]]) -> list[list[int]]: ``` ### Rules - `ids[i]` and `interests[i]` describe the same employee, so the two lists line up by index. - Two employees belong to the same group when a chain of employees links them and every neighboring pair in the chain shares at least one interest. The two ends of the chain need not share anything. - An employee whose interest list is empty, or whose interests nobody else has, is a group of one. - An interest repeated within one employee's list counts once. - Return the groups in a fixed order: sort the IDs inside each group in ascending order, then sort the groups in ascending order of their smallest ID. ### Constraints - `1 <= len(ids) == len(interests) <= 10^4` - `0 <= ids[i] <= 10^9`, and all values in `ids` are unique - `0 <= len(interests[i]) <= 20` - `1 <= len(interests[i][j]) <= 20` - `interests[i][j]` consists of lowercase English letters ### Examples ```text Input: ids = [1, 2, 3, 4, 5], interests = [["chess", "music"], ["music", "hiking"], ["painting"], ["hiking"], ["painting", "yoga"]] Output: [[1, 2, 4], [3, 5]] ``` Employees 1 and 2 share "music", and 2 and 4 share "hiking", so 1, 2 and 4 form one group even though 1 and 4 have no interest in common. Employees 3 and 5 share "painting" and form the other group. ```text Input: ids = [10, 20, 30], interests = [["a"], ["b"], ["c"]] Output: [[10], [20], [30]] ``` No two employees share an interest, so each employee forms a group alone. ```text Input: ids = [7, 3, 9, 5], interests = [["go", "rust"], [], ["rust"], ["go"]] Output: [[3], [5, 7, 9]] ``` Employee 7 shares "rust" with 9 and "go" with 5, so 5, 7 and 9 form one group, listed in ascending order. Employee 3 has no interests and is alone, and that group comes first because its smallest ID is lower.

Overview: Given employee IDs and a list of interests for each employee, return the groups of employees connected directly or through a chain of shared interests. It tests modeling the input as a graph, finding connected components efficiently when one interest is shared by thousands of people, and returning a deterministic sorted result.

Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

You are given n employees. The i-th employee has a unique ID ids[i] and a list of interests interests[i].

Two employees are directly connected if they share at least one common interest. Connections are transitive: if employee A is connected to B, and B is connected to C, then A, B and C all belong to the same group.

Return all groups of employees, where each group is a list of employee IDs. An employee who shares no interest with anyone else forms a group by themselves.

Function Signature

def group_employees(ids: list[int], interests: list[list[str]]) -> list[list[int]]:

Rules

  • ids[i] and interests[i] describe the same employee, so the two lists line up by index.
  • Two employees belong to the same group when a chain of employees links them and every neighboring pair in the chain shares at least one interest. The two ends of the chain need not share anything.
  • An employee whose interest list is empty, or whose interests nobody else has, is a group of one.
  • An interest repeated within one employee's list counts once.
  • Return the groups in a fixed order: sort the IDs inside each group in ascending order, then sort the groups in ascending order of their smallest ID.

Constraints

  • 1 <= len(ids) == len(interests) <= 10^4
  • 0 <= ids[i] <= 10^9 , and all values in ids are unique
  • 0 <= len(interests[i]) <= 20
  • 1 <= len(interests[i][j]) <= 20
  • interests[i][j] consists of lowercase English letters

Examples

Input: ids = [1, 2, 3, 4, 5], interests = [["chess", "music"], ["music", "hiking"], ["painting"], ["hiking"], ["painting", "yoga"]]
Output: [[1, 2, 4], [3, 5]]

Employees 1 and 2 share "music", and 2 and 4 share "hiking", so 1, 2 and 4 form one group even though 1 and 4 have no interest in common. Employees 3 and 5 share "painting" and form the other group.

Input: ids = [10, 20, 30], interests = [["a"], ["b"], ["c"]]
Output: [[10], [20], [30]]

No two employees share an interest, so each employee forms a group alone.

Input: ids = [7, 3, 9, 5], interests = [["go", "rust"], [], ["rust"], ["go"]]
Output: [[3], [5, 7, 9]]

Employee 7 shares "rust" with 9 and "go" with 5, so 5, 7 and 9 form one group, listed in ascending order. Employee 3 has no interests and is alone, and that group comes first because its smallest ID is lower.