Two engineers implemented union-find but their solution is slow on large inputs because they only added one of the two standard optimizations. Walk me through path compression versus union by rank, why you need both, and what goes wrong with only one.

technical-conceptual · Senior level · software-engineering

What the interviewer is really asking

Assesses whether the candidate understands the two distinct optimizations that give union-find its near-constant amortized cost, how each independently bounds tree height, and the senior skill of diagnosing a performance regression from a half-applied technique.

What to say

What to avoid

Example answers

Strong: Union by rank keeps the shorter tree under the taller one so a union never makes the tree taller than it must be — on its own that bounds height to O(log n). Path compression is in find(): once I reach the root, I repoint every node on the path straight to it, so the next find on that branch is O(1)-ish. Either alone is decent but not near-constant; you need both together to hit amortized O(alpha(n)). If a teammate's version is slow, the two things I'd check are whether find() actually mutates the parent pointers — a lot of people return the root but forget to repoint — and whether union compares rank or size instead of just hanging one root off the other, which is how you end up with linear chains.

Weak: I'd add path compression in find so the tree stays flat — that's the main optimization, so union by rank is kind of redundant once you have it.

Want questions matched to your role? Paste a job title, job description, or CV and get a personalized set, or go Pro to unlock the full bank.