Data Structures & AlgorithmsVery hardTCS NQT Digital · Product Company SDE

Problem: What is the time complexity of detecting a cycle in an undirected graph using Union-Find, processing E edges?

  1. A. O(E log E)
  2. B. O(E^2)
  3. C. O(V^3)
  4. D. O(E * alpha(V)), nearly O(E), where alpha is the inverse Ackermann function

Correct answer: D. O(E * alpha(V)), nearly O(E), where alpha is the inverse Ackermann function

With path compression and union by rank, each Union-Find operation runs in amortized O(alpha(V)) time (an extremely slow-growing function), so processing all E edges for cycle detection takes nearly-linear O(E * alpha(V)) time.

More Coding Interview Problems questions

Exams that ask this topic