Disjoint Set Union with Component Size and Sum Queries

Quick Overview

Implement a disjoint set union structure over n labeled elements that answers union, component size, and component sum queries. It tests union-find with path compression and union by rank, and keeping per-root metadata correct as components merge.

Disjoint Set Union with Component Size and Sum Queries

Company: Google

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Implement a disjoint set union (DSU, also called union-find) data structure that supports union and find operations, while also maintaining metadata for each component: the size of the component and the sum of all elements in the component. You are given `n` elements labeled from `0` to `n - 1`. Initially, each element is in its own component with size `1` and sum equal to its label value. You are given a list of `queries`. Each query is one of the following: - `[1, a, b]`: union the components containing elements `a` and `b`. Return the sum of the merged component. If `a` and `b` are already in the same component, return the sum of that component. - `[2, a, 0]`: return the size of the component containing element `a`. - `[3, a, 0]`: return the sum of the component containing element `a`. Return a list of results, one for each query, in query order. ### Function Signature ```python def dsu_with_metadata(n: int, queries: list[list[int]]) -> list[int]: ``` ### Rules - Each query is a list of three integers `[type, a, b]`. For types `2` and `3` the third value is `0` and is ignored. - An element's value is its label, so element `4` adds `4` to the sum of its component. - A union of an element with itself, or of two elements that are already connected, changes nothing and returns the current sum of their component. ### Constraints - `1 <= n <= 10^5` - `1 <= queries.length <= 10^5` - `queries[i].length == 3` - `queries[i][0]` is `1`, `2`, or `3` - `0 <= queries[i][1], queries[i][2] < n` ### Examples ```text Input: n = 5, queries = [[1,0,1],[1,2,3],[3,1,0],[2,3,0],[1,1,3],[3,0,0],[2,2,0]] Output: [1,5,1,2,6,6,4] ``` The first two unions build {0,1} with sum 0 + 1 = 1 and {2,3} with sum 2 + 3 = 5, so the sum query on 1 returns 1 and the size query on 3 returns 2. Joining 1 and 3 merges both into {0,1,2,3} with sum 6 and size 4, which the last two queries report. ```text Input: n = 3, queries = [[2,0,0],[3,2,0],[1,0,2],[2,0,0],[3,0,0]] Output: [1,2,2,2,2] ``` Before any union, element 0 is alone (size 1) and element 2 has sum 2. The union of 0 and 2 gives {0,2} with sum 0 + 2 = 2 and size 2. ```text Input: n = 4, queries = [[1,0,3],[1,3,0],[2,0,0],[3,1,0]] Output: [3,3,2,1] ``` The first union builds {0,3} with sum 3. The second query names two elements that are already connected, so it returns the same sum without changing anything. Element 1 was never merged, so its sum is its own label.

Overview: Implement a disjoint set union structure over n labeled elements that answers union, component size, and component sum queries. It tests union-find with path compression and union by rank, and keeping per-root metadata correct as components merge.

Google logo
Google
Nov 25, 2026
mediumSoftware EngineerOtherCoding & Algorithms
1
0

Implement a disjoint set union (DSU, also called union-find) data structure that supports union and find operations, while also maintaining metadata for each component: the size of the component and the sum of all elements in the component.

You are given n elements labeled from 0 to n - 1. Initially, each element is in its own component with size 1 and sum equal to its label value.

You are given a list of queries. Each query is one of the following:

  • [1, a, b] : union the components containing elements a and b . Return the sum of the merged component. If a and b are already in the same component, return the sum of that component.
  • [2, a, 0] : return the size of the component containing element a .
  • [3, a, 0] : return the sum of the component containing element a .

Return a list of results, one for each query, in query order.

Function Signature

def dsu_with_metadata(n: int, queries: list[list[int]]) -> list[int]:

Rules

  • Each query is a list of three integers [type, a, b] . For types 2 and 3 the third value is 0 and is ignored.
  • An element's value is its label, so element 4 adds 4 to the sum of its component.
  • A union of an element with itself, or of two elements that are already connected, changes nothing and returns the current sum of their component.

Constraints

  • 1 <= n <= 10^5
  • 1 <= queries.length <= 10^5
  • queries[i].length == 3
  • queries[i][0] is 1 , 2 , or 3
  • 0 <= queries[i][1], queries[i][2] < n

Examples

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

The first two unions build {0,1} with sum 0 + 1 = 1 and {2,3} with sum 2 + 3 = 5, so the sum query on 1 returns 1 and the size query on 3 returns 2. Joining 1 and 3 merges both into {0,1,2,3} with sum 6 and size 4, which the last two queries report.

Input: n = 3, queries = [[2,0,0],[3,2,0],[1,0,2],[2,0,0],[3,0,0]]
Output: [1,2,2,2,2]

Before any union, element 0 is alone (size 1) and element 2 has sum 2. The union of 0 and 2 gives {0,2} with sum 0 + 2 = 2 and size 2.

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

The first union builds {0,3} with sum 3. The second query names two elements that are already connected, so it returns the same sum without changing anything. Element 1 was never merged, so its sum is its own label.