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
Coding & Algorithms
Software Engineer
HubSpot
November 9, 2025
Software Engineer
Technical Screen
Coding & Algorithms
Medium

4

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
Tree structures and recursion
Time and space complexity analysis
Binary search and divide and conquer
Dynamic programming and memoization
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 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 Problems
Sample 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
  1. Data Structures: We will maintain two arrays: parent and rank. The parent array will track the root of each element, while the rank array will track the depth of trees for union operati...

Submit Your Answer
Markdown supported

Related Questions