Skip to question
ASIC.FYI
DesignVerificationSystemVerilogFirmwareArchitectureInterviews
ASIC.FYI/Interview questions/Dynamic Connectivity with Disjoint Sets

Q237·Free·Firmware

Dynamic Connectivity with Disjoint Sets

Difficulty
Medium
Topic
Algorithms
Language
SV
Interview prompt

Question

Track connectivity among sparsely numbered components as links are added over time. Queries should remain fast even after a long sequence of unions.

Starting point

Question code

class ConnectivityTracker;
  bit add_node(int id);
  bit connect(int a, int b);
  bit connected(int a, int b);
  int component_size(int id);
endclass
Reviewed example

Trace one case

Input
add_node(0); add_node(1); add_node(2); add_node(3); connect(0,1); connect(2,3); connected(0,3); connect(1,2); connected(0,3)
Expected output
false, then true

The final connection joins the two components; path compression and union by size preserve near-constant amortized operations.

What to cover

Requirements

  1. Reject or clearly define operations on unknown node IDs.
  2. Duplicate links and self-links must not corrupt component sizes.
  3. Use path compression and union by rank or size.
  4. connect returns 1 only when two previously separate components are joined.
Exact question handoffPractice Q237

Solve it in the question bank, keep your progress, and reveal the reviewed solution when your access allows.

Open in question bank →
Solution accessA Free account unlocks the complete reviewed solution.
Continue learning

Firmware

  • Algorithms
  • Union-find
  • Associative array
  • Path compression
Firmware interview questions →
Continue practicing

Related questions

Q009 · Reference ModelsMatch out-of-order packets by ID→Q208 · Data StructuresInsert, remove, and sample in average O(1)→Q270 · StringsClean palindrome check→
ASIC.FYI · Learn silicon end to end.info@asic.fyi