Data Structures & AlgorithmsHardProduct Company SDE · CP practice

Problem: What is the number of steps for the Collatz sequence starting at 97 to reach 1? Run the code.

Problem: What is the number of steps for the Collatz sequence starting at 97 to reach 1? Run the code.
n = 97
c = 0
while n != 1:
    n = n // 2 if n % 2 == 0 else 3 * n + 1
    c += 1
print(c)
What is printed?
  1. A. 119
  2. B. 118
  3. C. 75
  4. D. 43

Correct answer: B. 118

Simulation takes 118 steps (75 halvings and 43 tripling steps). Counting the terms including 97 gives 119, while 75 and 43 count only one kind of step.

More Coding Interview Problems questions

Exams that ask this topic