🎙 Free live masterclass · Sun, 29 Nov, 7:00 pm IST · Java Full Stack roadmap Register free →
Skills101
← Quiz: Topological Sort: all 15 questions

Quiz: Topological Sort · Question 9 of 15

What does this print? (code)

What does this print?

edges = {"a": ["b"], "b": ["c"], "c": ["a"], "d": []}
color = {k: 0 for k in edges}
def has_cycle(u):
    color[u] = 1
    for v in edges[u]:
        if color[v] == 1 or (color[v] == 0 and has_cycle(v)):
            return True
    color[u] = 2
    return False
print(any(color[n] == 0 and has_cycle(n) for n in edges))
  1. A.True
  2. B.False
  3. C.None
  4. D.Error
Answer & explanation

Correct answer: True

a → b → c → a reaches the gray node a again, so a cycle is reported.

Can you get the rest right without peeking?

Take the timed Quiz: Topological Sort quiz free. Pass it to earn XP and count towards your certificate.

Start the quiz →