← Quiz: Union-Find (Disjoint Set Union): all 10 questions
Quiz: Union-Find (Disjoint Set Union) · Question 2 of 10
What does this print? (code)
What does this print?
parent = list(range(5))
def find(x):
while parent[x] != x:
x = parent[x]
return x
def union(a, b):
parent[find(a)] = find(b)
union(0, 1)
union(2, 3)
union(1, 3)
print(find(0) == find(2), find(4))
- A.False 4
- B.True 3
- C.True 4
- D.False 0
Answer & explanation
Correct answer: True 4
0 joins 1, 2 joins 3, then 1's root joins 3's root, so 0 and 2 share root 3. Element 4 is still alone.
Can you get the rest right without peeking?
Take the timed Quiz: Union-Find (Disjoint Set Union) quiz free. Pass it to earn XP and count towards your certificate.
Start the quiz →