KatInterview

Disjoint Set: Merge Nodes and Query Connectivity

Data Structure DesignAuto-graded · 18 test cases
MediumAuto-gradedData Structure Designunion-finddisjoint-setgraph

Last updated

Tests ability to implement Union-Find with union by rank and path compression, supporting merge, connectivity, and component-count queries.

Disjoint Set: Merge Nodes and Query Connectivity

You have n nodes labeled 0 through n - 1, each initially its own isolated component. Your task is to build a data structure that efficiently handles three kinds of operations issued in sequence, then return the collected results.

Operations

NameArgumentsWhat to return
"union"[a, b]the number of connected components after performing the union, whether or not a and b were already in the same component
"connected"[a, b]True if a and b are in the same component, False otherwise
"count"[]the current number of connected components

Only these three operation names will appear. Every node index is guaranteed to be in range 0..n-1, and each operation is given the correct number of arguments.

Collect the return value of every operation into a list and return it.

Implementation Requirements

Your internal find routine must use path compression. Path halving — redirecting each visited node to its grandparent — is an accepted variant. Your union routine must apply union by rank: always attach the lower-rank root beneath the higher-rank root, and increment the rank only when two roots of equal rank are merged.

Constraints

  • 0 <= n <= 10^4
  • 1 <= len(operations) == len(arguments) <= 10^4
  • Each operation is one of "union", "connected", "count"
  • For "union" and "connected", arguments[i] contains exactly two integers, both in [0, n-1]
  • For "count", arguments[i] is an empty list
  • a == b is allowed; applying "union" to the same node twice is a no-op but still appends the current component count
  • n = 0 is valid; "count" returns 0

Examples

Example 1

Input: run_component_queries(5, ['count', 'union', 'union', 'connected', 'union', 'connected', 'count'], [[], [0, 1], [1, 2], [0, 2], [3, 4], [2, 4], []])

Output: [5, 4, 3, True, 2, False, 2]

Example 2

Input: run_component_queries(4, ['connected', 'union', 'connected', 'union', 'union', 'connected'], [[0, 1], [0, 1], [0, 1], [1, 2], [2, 3], [0, 3]])

Output: [False, 3, True, 2, 1, True]

Example 3

Input: run_component_queries(1, ['count', 'union', 'connected'], [[], [0, 0], [0, 0]])

Output: [1, 1, True]

Hints

Hint 1

Track the number of components as a single integer. It starts at n and decreases by exactly 1 each time a union actually joins two previously separate components — a same-root union changes nothing but still appends the current count.

Hint 2

In your find routine, walk up the parent chain using path halving: set parent[x] = parent[parent[x]] before advancing x. This flattens the tree in a single pass without recursion, which also avoids Python's recursion limit on long chains.

Hint 3

For union by rank, compare the ranks of the two roots. Attach the smaller-rank root under the larger-rank root. Only when both ranks are equal do you increment the surviving root's rank by 1.