← Quiz: Union-Find (Disjoint Set Union): all 10 questions
Quiz: Union-Find (Disjoint Set Union) · Question 7 of 10
What does this print? (code)
What does this print?
parent = list(range(4))
def find(x):
while parent[x] != x:
x = parent[x]
return x
cycle = False
for u, v in [(0, 1), (1, 2), (0, 2)]:
a, b = find(u), find(v)
if a == b:
cycle = True
parent[a] = b
print(cycle)
- A.True
- B.False
- C.None
- D.Error
Answer & explanation
Correct answer: True
After 0–1 and 1–2, the edge 0–2 joins two nodes that are already connected, closing a cycle.
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 →