Data Structures & AlgorithmsModerateProduct Company SDE · union-find

Problem: What is the purpose of path compression in a union-find structure?

  1. A. To sort elements
  2. B. To count connected components only
  3. C. To flatten the tree so later finds are nearly constant time
  4. D. To compute distances

Correct answer: C. To flatten the tree so later finds are nearly constant time

Pointing visited nodes directly at the root shortens future find operations.

More Coding Interview Problems questions

Exams that ask this topic