‹ DS&A interview · Socratic
DSA · Math · #69

Factorial Trailing Zeroes

Module 58 · difficulty 3/5·30:00starts on first keystroke

Given an integer `n`, return the number of trailing zeroes in `n!` (`n` factorial). A trailing zero is produced by a factor of 10, i.e. a pair of factors 2 and 5. Implement `trailingZeroes(n)` which returns this count as an integer. Aim for `O(log n)` time without computing the factorial itself.

Examples
  • n = 3 03! = 6, no trailing zero.
  • n = 5 15! = 120, one trailing zero.
  • n = 0 00! = 1.
Constraints
  • · 0 <= n <= 10^4
Session phases
A · Clarify
B · Approach
C · Complexity
D · Edges
E · Code
F · Tradeoff
G · Score
Phase A — Clarify
Ask questions about input bounds, types, and edge constraints.
Ask the coach clarifying questions about the problem.
When you've covered this phase, advance to the next.