Union-Find (Disjoint Set) Interview Questions: Complete Guide (2026)

What union-find interview questions actually test, which problem categories appear most at top companies, and how LeakCode's data from 51,000+ reports helps you prepare the right depth.

Why union-find appears in FAANG interviews

Union-find occupies a specific niche in algorithm interviews: it is the most efficient known solution for dynamic connectivity problems, and implementing it correctly from scratch (with path compression and union by rank) demonstrates fluency with pointer-style data structure manipulation, amortized analysis awareness, and graph reasoning.

In LeakCode's database of 51,000+ real interview reports, union-find questions are more specialized than graph traversal questions but appear consistently in mid and senior-level coding rounds at Google, Meta, and Amazon. The data shows that candidates who implement union-find with both optimizations pass at significantly higher rates than those who implement a naive version, even when both pass all test cases.

A particularly common interview pattern is using union-find inside a larger problem (grid processing, account merging, network monitoring) where the candidate must recognize that connectivity is the core subproblem before they can select the right data structure.

Main union-find problem categories

Connected components counting

Count the number of distinct connected components in a graph or grid. Initialize each node as its own component. For each edge, union the two endpoints. The number of unique roots at the end equals the component count. Solvable with BFS or DFS as well, but union-find is more efficient when edges are added incrementally. Commonly asked at Amazon and Microsoft.

Cycle detection in undirected graphs

Detect whether adding an edge creates a cycle. If both endpoints of a new edge already belong to the same component (find returns the same root), adding the edge would create a cycle. Used in Kruskal's MST algorithm to skip edges that would form cycles. Appears in redundant connection problems, network design questions, and dependency graph analysis.

Minimum spanning tree (Kruskal's algorithm)

Sort edges by weight. For each edge in order, union the two endpoints if they are not already connected. The first n-1 edges that do not form a cycle form the MST. Union-find makes the cycle check O(alpha(n)) per edge. Understanding both Kruskal's (edge-centric, union-find) and Prim's (vertex-centric, heap) and knowing when each is preferred is expected in senior-level graph rounds.

Dynamic grid connectivity

Problems where land or connections are added to a grid dynamically and you must track connected regions after each addition. Number of islands II is the canonical example: cells are added one by one and you must report the island count after each addition. Union-find handles this in O(alpha(n)) per addition versus O(n) for a full BFS after each step. Heavily reported in Google and Meta hard rounds.

Entity grouping and account merging

Group accounts or identities that share a common attribute (same email address, same phone number) into unified records. For each shared attribute, union the associated accounts. The connected components at the end are the merged accounts. This disguises union-find as a data processing problem and is a common Amazon and Meta interview variant that tests whether candidates recognize the underlying graph structure.

How to implement union-find correctly

A minimal correct implementation requires three components: an array mapping each element to its parent, a rank or size array for union by rank, and the two operations with their optimizations applied.

  1. 1.Path compression in find. When finding the root, set every node along the path to point directly to the root. One-liner recursive: parent[x] = find(parent[x]). This flattens the tree for future queries.
  2. 2.Union by rank or size. When merging two trees, always attach the root with smaller rank or size under the root with larger rank or size. This keeps trees shallow and prevents the worst-case O(n) find.
  3. 3.Track component count if needed. Initialize a count variable equal to n. Decrement it every time a union actually merges two distinct components (i.e., find(a) != find(b) before the union). Return this count for component counting problems.
  4. 4.State the amortized complexity. With both optimizations, operations are effectively O(1). Stating this explicitly shows familiarity with the algorithm beyond basic implementation.

Companies that ask union-find questions

Browse real union-find interview questions from these companies on LeakCode:

Search Union-Find on LeakCode

LeakCode has 51,000+ real interview questions from 2,000+ companies. Filter by company to see which union-find problem categories your target company asks most often.

Browse Union-Find Questions

How LeakCode helps with union-find prep

LeakCode indexes real candidate-reported questions from 1Point3Acres, Blind, LeetCode Discuss, Glassdoor, and Reddit. Filtering by company shows you the distribution of union-find problem types your target company uses, so you know whether to focus on pure connectivity problems or disguised graph union-find variants.

See also: how LeakCode works, our data sources, and FAQ. Related guides: graph interview questions, DFS and BFS interview questions, and greedy algorithm interview questions.