const solve = (nums) => { let left = 0, right = nums.length - 1; while (left < right) { const sum = nums[left] + nums[right]; if (sum === target) return [left, right]; }}
Patterns

Union Find LeetCode Pattern: The Complete Guide

Union-Find (Disjoint Set Union) is the elegant data structure that solves connected component problems in near-constant time — here is the template and the problems where it is the optimal choice.

10 min read|

Union-Find solves connected component problems in near-constant time

The elegant data structure template for grouping, merging, and connectivity

Union Find: The Data Structure Interviewers Love

Union-Find, also known as Disjoint Set Union (DSU), is one of the most elegant data structures in computer science — and one of the most underrated in interview prep. While most candidates spend weeks grinding BFS and DFS problems, union find leetcode problems quietly appear in medium and hard questions across arrays, graphs, and even string categories. If you have never seen the pattern before, these problems feel impossibly hard. Once you learn the template, they become some of the easiest problems to solve.

The core idea is deceptively simple: you have a collection of elements, and you need to efficiently group them together and check whether two elements belong to the same group. That is it. Two operations — union (merge two groups) and find (which group does this element belong to?) — and with the right optimizations, both run in nearly constant time.

Union-Find shows up in coding interviews at Google, Meta, Amazon, and Microsoft with surprising frequency. It is the optimal solution for problems involving connected components, redundant connections, cycle detection in undirected graphs, and dynamic connectivity queries. If you are serious about interview prep, this is a pattern you cannot afford to skip.

What Is Union Find? The Two Operations That Power Everything

At its heart, the union find algorithm is a data structure that tracks which elements belong to the same group — also called a "disjoint set." You start with n elements, each in its own group. Then you process a series of operations: merge two groups together (union), or ask whether two elements are in the same group (find). The magic is that both operations can be made nearly O(1) with two simple optimizations.

The data structure uses a parent array where each element points to its "parent" in the group. Initially, every element is its own parent — meaning it is the representative (or root) of its own one-element group. When you union two elements, you connect one root to the other, merging the groups. When you find which group an element belongs to, you follow parent pointers until you reach a root.

Without optimizations, this could degenerate into a linked list with O(n) find operations. But with path compression and union by rank, you get amortized near-constant time for every operation. This is what makes Union-Find so powerful — it handles millions of union and find operations in essentially linear total time.

  • Find(x): Follow parent pointers from x until you reach the root — that root is the group representative
  • Union(x, y): Find the roots of x and y, then make one root point to the other
  • Path compression: During find, make every node on the path point directly to the root
  • Union by rank: Always attach the shorter tree under the taller tree to keep depth minimal

The Union Find Template: 15 Lines You Should Memorize

The entire union find implementation fits in roughly 15 lines of code, and every competitive programmer and interview candidate should have it committed to memory. The template consists of three parts: initialization, find with path compression, and union by rank. Once you have this template, you can drop it into any union find leetcode problem and focus on the problem-specific logic.

Initialization is straightforward: create a parent array where parent[i] = i (every element is its own root) and a rank array initialized to zero. The find function recursively follows parent pointers and applies path compression by setting each visited node to point directly to the root. The union function finds both roots, compares ranks, and attaches the smaller tree to the larger one.

Here is the mental model: parent[i] = i means "I am my own boss." parent[i] = j means "j is my boss." Find follows the chain of bosses until you reach someone who is their own boss — that is the group leader. Path compression is like telling HR "skip all the middle managers, just report directly to the CEO." Union by rank ensures the CEO of the bigger company always stays on top when two companies merge.

The time complexity with both optimizations is O(alpha(n)) per operation, where alpha is the inverse Ackermann function. For all practical purposes — even with billions of elements — alpha(n) is at most 4. This is effectively constant time, which is why Union-Find outperforms BFS and DFS for connectivity problems.

💡

Pro Tip

The entire Union-Find implementation is ~15 lines of code — memorize the template with path compression and union by rank. It's short enough to write from memory in an interview.

When to Use Union Find vs BFS/DFS

One of the most common mistakes in interviews is reaching for BFS or DFS when Union-Find is the better tool — or vice versa. Both can solve connectivity problems, but they have different strengths. Knowing when to use which approach is what separates a good answer from an optimal one.

Union-Find wins when edges arrive dynamically and you need to answer connectivity queries as you go. If the problem says "process these edges one at a time and tell me when two nodes become connected," that is Union-Find territory. It also wins when you need to count connected components efficiently, detect cycles in undirected graphs, or when the graph is too large to build an adjacency list.

BFS and DFS win when you need shortest paths, specific traversal orders, or when you need to explore neighborhoods layer by layer. If the problem asks for the minimum number of steps, the shortest path between two nodes, or requires you to process nodes in a specific order (topological sort, level-order), Union-Find cannot help — use BFS or DFS instead.

  • Use Union-Find: dynamic edge processing, connectivity queries, cycle detection in undirected graphs, counting components, Kruskal MST
  • Use BFS: shortest path in unweighted graphs, level-order traversal, minimum steps problems
  • Use DFS: path finding, exhaustive search, topological sort, strongly connected components, backtracking
  • Both work: static connected components — but Union-Find is often cleaner and faster for this case

Classic Union Find LeetCode Problems You Must Know

There is a core set of union find leetcode problems that appear repeatedly in interviews. Mastering these five problems gives you the pattern recognition to handle virtually any Union-Find variant you encounter. Start with these before moving to advanced applications.

Number of Connected Components in an Undirected Graph (LeetCode #323) is the purest Union-Find problem. You are given n nodes and a list of edges — count the connected components. Initialize n components, then for each edge, union the two endpoints. Every successful union reduces the component count by one. This is the first problem you should solve to lock in the template.

Redundant Connection (LeetCode #684) asks you to find the edge that, when removed, turns a graph with a cycle back into a tree. Process edges one by one with Union-Find. When you try to union two nodes that already have the same root, that edge creates a cycle — return it. This is the classic cycle detection pattern.

Accounts Merge (LeetCode #721) is a harder application where you union email addresses belonging to the same person. The trick is mapping emails to indices, then unioning all emails within each account. Finally, group emails by their root representative. This problem tests whether you can adapt the template to non-integer elements.

Longest Consecutive Sequence (LeetCode #128) can be solved with Union-Find by unioning consecutive numbers together and tracking group sizes. While a hash set approach also works in O(n), the Union-Find solution demonstrates how the data structure applies beyond traditional graph problems.

Number of Islands (LeetCode #200) is typically solved with DFS or BFS, but the Union-Find approach is an excellent exercise. Treat each land cell as a node and union adjacent land cells. The number of distinct roots at the end equals the number of islands. This alternative approach is especially useful in follow-up questions about streaming or dynamic grid updates.

ℹ️

Complexity Deep Dive

Union-Find with path compression and union by rank achieves an amortized time complexity of O(alpha(n)) per operation — where alpha is the inverse Ackermann function, effectively constant for all practical inputs.

Advanced Union Find Patterns for Hard Problems

Once you have the basics down, there are several advanced Union-Find patterns that appear in hard-level problems and competitive programming. These extensions build on the same template but add extra bookkeeping to handle more complex requirements.

Weighted Union-Find attaches weights to edges in the parent array. When you union two elements, you also record the relative weight between them. During find with path compression, you update weights along the path. This pattern solves problems like Evaluate Division (LeetCode #399), where you need to compute ratios between variables connected by equations.

Union-Find with rollback (also called "offline Union-Find") supports undo operations by using union by rank without path compression and maintaining a stack of changes. This is primarily a competitive programming technique, but it demonstrates the flexibility of the data structure. In interviews, you are unlikely to need rollback, but understanding it shows deep knowledge.

Kruskal's algorithm for Minimum Spanning Tree is one of the most important real-world applications of Union-Find. Sort all edges by weight, then process them in order. For each edge, use Union-Find to check whether adding it would create a cycle. If not, include it in the MST. This greedy approach with Union-Find runs in O(E log E) time and is often simpler to implement than Prim's algorithm.

  • Weighted Union-Find: track relative weights between elements — used in ratio/equation problems
  • Union-Find with rollback: undo operations using a change stack — competitive programming technique
  • Kruskal MST: sort edges by weight, use Union-Find for cycle detection — O(E log E) total
  • Union-Find with size tracking: maintain group sizes for problems that ask about component sizes

Practice Strategy: From Template to Mastery

The best way to master union find for interviews is a structured three-phase approach. Phase one: memorize the template. Write the parent array initialization, find with path compression, and union by rank from memory until you can do it in under two minutes. This is your foundation — every problem builds on it.

Phase two: solve the five classic problems listed above in order. Start with Number of Connected Components (#323) to validate your template works. Move to Redundant Connection (#684) for cycle detection. Then tackle Accounts Merge (#721) to practice adapting the template to non-integer inputs. Longest Consecutive Sequence (#128) and Number of Islands (#200) round out your core problem set.

Phase three: attempt one or two advanced problems like Evaluate Division (#399) or Smallest String With Swaps (#1202). These test whether you truly understand the data structure or are just pattern-matching. If you can solve these without looking at hints, you are interview-ready for Union-Find.

Use YeetCode flashcards to reinforce the pattern between practice sessions. The key to retaining Union-Find knowledge is spaced repetition — reviewing the template and key problem approaches at increasing intervals. Most candidates learn Union-Find once and forget it by interview day. Flashcard-based review ensures the pattern stays fresh when you need it most.

⚠️

Important

Union-Find only works on undirected graphs — if the problem involves directed edges, you need a different approach (topological sort, DFS with states). Check edge directionality before reaching for Union-Find.

Ready to master algorithm patterns?

YeetCode flashcards help you build pattern recognition through active recall and spaced repetition.

Start practicing now