Implement Union Find with streaming input

Last updated: April 6, 2026

Quick Overview

Implement a Union-Find (also known as Disjoint Set Union) data structure that can handle streaming input, allowing for union and find operations in nearly constant time. Your implementation should support dynamic input where elements can be added on-the-fly, and it should be able to efficiently determine the connected components among the elements. Ensure that your solution can handle a sequence of union and find operations and return the results of the find operations as output.

Cockroach Labs
Coding & Algorithms
Machine Learning Engineer
Cockroach Labs
April 6, 2026
Machine Learning Engineer
Phone Screen
Coding & Algorithms
Medium

13

3

771 solved


Implement a Union-Find (also known as Disjoint Set Union) data structure that can handle streaming input, allowing for union and find operations in nearly constant time. Your implementation should support dynamic input where elements can be added on-the-fly, and it should be able to efficiently determine the connected components among the elements. Ensure that your solution can handle a sequence of union and find operations and return the results of the find operations as output.

Cockroach Labs uses this problem in the Phone Screen to evaluate your algorithmic thinking. They expect you to discuss multiple approaches, analyze trade-offs between them, and implement the optimal solution with clean, readable code.

What the Interviewer Expects
  • Recognize the underlying problem pattern (sliding window, two pointers, BFS/DFS, etc.)
  • Discuss multiple approaches and trade-offs before coding
  • Implement an optimal solution with clean, production-quality code
  • Handle all edge cases including boundary conditions and invalid input
  • Optimize both time and space complexity with clear justification
  • Test your solution systematically with well-chosen examples
Key Topics to Cover
Common algorithm patterns (sliding window, two pointers, BFS/DFS)
Graph algorithms and traversal
Data structure selection and trade-offs
Hash maps and frequency counting
Binary search and divide and conquer
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How would you parallelize this solution?
  • Can you solve this iteratively instead of recursively (or vice versa)?
  • Can you solve this in a single pass?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Problem Analysis

The problem requires the implementation of a Union-Find data structure that can handle dynamic input. The Union-Find, or Disjoint Set Union (DSU), is efficient for managing and merging sets of element...

Approach
  1. Data Structure Setup: Initialize two dictionaries: parent to store the parent of each element and rank to keep track of the tree depth for union operations.
  2. Union Operation: To union...

Submit Your Answer
Markdown supported

Related Questions