how many trailing zeroes are there in 100! (100 factorial)?
Solution
One per factor of 10, and one per factor of 5 (there are more than enough 2’s to pair with the 5’s), plus one per factor of ten squared (one occurrence) and one per factor of 5 squared (three occurrences).
So if I’m counting correctly, that’d be 10 + 10 + 1 + 3== 24 zeroes.
Assuming the question meant *trailing* zeroes. It’d be much harder to also count the intermingled zero digits in the entire expansion.
💡Strategies for Solving This Problem
Recursion and Large Numbers
This showed up at Amazon in 2024. Seems simple but tests recursion, overflow handling, and optimization thinking.
The Problem
Calculate 100! (100 factorial). That's 100 × 99 × 98 × ... × 2 × 1.
Challenge: Result has 158 digits. Standard integers overflow. Need BigInt or custom solution.
Approach 1: Simple Recursion
Classic factorial:
factorial(n) = n × factorial(n-1) factorial(0) = 1
O(n) time, O(n) space for call stack. Works but not optimized.
Approach 2: Iterative
Loop from 1 to n, multiply. Same O(n) time but O(1) space. Better.
Approach 3: Tail Recursion
Convert to tail-recursive form so compiler can optimize to iteration. Some languages (not JavaScript) optimize this automatically.
The Real Challenge: BigInt
100! = 93326215443944152681699238856266700490715968264381621468592963895217599993229915608941463976156518286253697920827223758251185210916864000000000000000000000000
That's 158 digits. Need arbitrary precision arithmetic.
JavaScript has BigInt built-in (ES2020+). Other languages need libraries or manual implementation.
Optimization: Memoization
If calculating multiple factorials, cache results. factorial(100) uses factorial(99), etc.
At Amazon
Started with simple recursion. Interviewer asked "What about overflow?" That's when I switched to BigInt. Then asked "How to optimize for many queries?" Showed memoization.
Also discussed: How many trailing zeros? (24 - count factors of 5)