Count conglomerates formed by company ownership relationships

Quick Overview

Given n companies and a list of undirected ownership links, count how many separate conglomerates the links form, where a company with no links counts as its own group. It tests modeling a problem as a graph and counting connected components with union-find or a traversal.

Count conglomerates formed by company ownership relationships

Company: Grab

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

There are `n` companies in a market, labeled from `0` to `n - 1`. Some companies have ownership relationships with one another, where each relationship `[a, b]` indicates that company `a` and company `b` belong to the same conglomerate. A conglomerate is a group of companies that are directly or indirectly connected through ownership relationships. Given the integer `n` and a list of `relationships`, return the total number of distinct conglomerates in the market. A company with no relationships forms its own conglomerate of size one. ### Function Signature ```python def count_conglomerates(n: int, relationships: list[list[int]]) -> int: ``` ### Rules - Relationships are undirected: `[a, b]` and `[b, a]` describe the same link. - Every company from `0` to `n - 1` counts, including companies that appear in no relationship. ### Constraints - `1 <= n <= 10^4` - `0 <= len(relationships) <= min(n * (n - 1) / 2, 10^5)` - `len(relationships[i]) == 2` - `0 <= a, b < n` for every relationship `[a, b]` - `a != b` - There are no duplicate relationships. ### Examples ```text Input: n = 5, relationships = [[0, 1], [1, 2], [3, 4]] Output: 2 ``` Companies 0, 1 and 2 are connected through company 1, and companies 3 and 4 are connected to each other. That makes two conglomerates. ```text Input: n = 4, relationships = [] Output: 4 ``` With no relationships, each company is its own conglomerate. ```text Input: n = 6, relationships = [[0, 5], [2, 3], [5, 3], [0, 2]] Output: 3 ``` The first three links join companies 0, 5, 3 and 2 into one group, and `[0, 2]` links two companies already in that group. Companies 1 and 4 stand alone, so the total is 3.

Overview: Given n companies and a list of undirected ownership links, count how many separate conglomerates the links form, where a company with no links counts as its own group. It tests modeling a problem as a graph and counting connected components with union-find or a traversal.

Grab logo
Grab
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

There are n companies in a market, labeled from 0 to n - 1. Some companies have ownership relationships with one another, where each relationship [a, b] indicates that company a and company b belong to the same conglomerate.

A conglomerate is a group of companies that are directly or indirectly connected through ownership relationships. Given the integer n and a list of relationships, return the total number of distinct conglomerates in the market.

A company with no relationships forms its own conglomerate of size one.

Function Signature

def count_conglomerates(n: int, relationships: list[list[int]]) -> int:

Rules

  • Relationships are undirected: [a, b] and [b, a] describe the same link.
  • Every company from 0 to n - 1 counts, including companies that appear in no relationship.

Constraints

  • 1 <= n <= 10^4
  • 0 <= len(relationships) <= min(n * (n - 1) / 2, 10^5)
  • len(relationships[i]) == 2
  • 0 <= a, b < n for every relationship [a, b]
  • a != b
  • There are no duplicate relationships.

Examples

Input: n = 5, relationships = [[0, 1], [1, 2], [3, 4]]
Output: 2

Companies 0, 1 and 2 are connected through company 1, and companies 3 and 4 are connected to each other. That makes two conglomerates.

Input: n = 4, relationships = []
Output: 4

With no relationships, each company is its own conglomerate.

Input: n = 6, relationships = [[0, 5], [2, 3], [5, 3], [0, 2]]
Output: 3

The first three links join companies 0, 5, 3 and 2 into one group, and [0, 2] links two companies already in that group. Companies 1 and 4 stand alone, so the total is 3.