Data Structures & AlgorithmsHardProduct Company SDE · Amazon OA

Problem: Nine nodes 0 to 8 start with no edges. These union operations are applied in order: (0,1), (2,3), (1,2), (4,5), (6,7), (7,8), (5,8), (0,3), (2,6). An operation is "redundant" if its two endpoints are already in the same component when it is applied. How many operations are redundant?

  1. A. 2
  2. B. 0
  3. C. 1
  4. D. 3

Correct answer: C. 1

Replay the unions with a disjoint-set structure and count the ones that hit an already-merged pair. Only (0,3) closes a loop, since 0 and 3 were already joined; (2,6) merges two separate groups, so the count is 1.

More Coding Interview Problems questions

Exams that ask this topic