ProgrammingHardCodeforces-style · placement coding round

What is printed?

What is printed?
def steps_to_cycle(f, s):
    seen = {}
    i = 0
    while s not in seen:
        seen[s] = i
        s = f[s]
        i += 1
    return i, i - seen[s]
print(steps_to_cycle({0: 1, 1: 2, 2: 3, 3: 1}, 0))
  1. A. (3, 3)
  2. B. (4, 4)
  3. C. (4, 3)
  4. D. (3, 2)

Correct answer: C. (4, 3)

The walk 0, 1, 2, 3 reaches the 1-2-3 cycle. The first repeat is at node 1 on step 4, and the cycle length is 4 - 1 = 3.

More Python Programming questions

Exams that ask this topic