Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: What is the number of trailing zeros in 30!? Run the code.

Problem: What is the number of trailing zeros in 30!? Run the code.
n = 30
c = 0
p = 5
while p <= n:
    c += n // p
    p *= 5
print(c)
What is printed?
  1. A. 7
  2. B. 6
  3. C. 3
  4. D. 26

Correct answer: A. 7

Zeros come from factors of 5: 30 // 5 + 30 // 25 = 6 + 1 = 7, with 25 giving an extra 5. Stopping at multiples of 5 gives 6, 30 // 10 gives 3, and 26 counts factors of 2, which are not the scarce ones.

More Coding Interview Problems questions

Exams that ask this topic