Implement Union Find with without recursion
Last updated: November 9, 2025
Quick Overview
Implement the Union Find data structure (also known as Disjoint Set Union) without using recursion. Your implementation should support the union and find operations efficiently, with path compression and union by rank, and should handle a series of union and find queries as input. The output should indicate the results of the find operations, showing which elements are connected.
HubSpot
November 9, 20254
3
4,807 solved
Implement the Union Find data structure (also known as Disjoint Set Union) without using recursion. Your implementation should support the union and find operations efficiently, with path compression and union by rank, and should handle a series of union and find queries as input. The output should indicate the results of the find operations, showing which elements are connected.
HubSpot uses this problem in the Technical 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
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you test this solution thoroughly?
- How would you modify your solution to handle streaming input?
- What is the worst-case input for your solution?
- How would your solution change if the input was sorted?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Problem Analysis
The problem requires us to implement a Union-Find (Disjoint Set Union) data structure without recursion. The Union-Find structure is commonly used to manage and merge disjoint sets efficiently. The ke...
Approach
- Data Structures: We will maintain two arrays:
parentandrank. Theparentarray will track the root of each element, while therankarray will track the depth of trees for union operati...