Overflow in intermediate arithmetic
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.
Move from brute-force thinking to an efficient approach using math strategy.
Given an integer n, return the number of trailing zeroes in n!.
Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Example 1:
Input: n = 3 Output: 0 Explanation: 3! = 6, no trailing zero.
Example 2:
Input: n = 5 Output: 1 Explanation: 5! = 120, one trailing zero.
Example 3:
Input: n = 0 Output: 0
Constraints:
0 <= n <= 104Follow up: Could you write a solution that works in logarithmic time complexity?
Problem summary: Given an integer n, return the number of trailing zeroes in n!. Note that n! = n * (n - 1) * (n - 2) * ... * 3 * 2 * 1.
Start with the most direct exhaustive search. That gives a correctness anchor before optimizing.
Pattern signal: Math
3
5
0
number-of-digit-one)preimage-size-of-factorial-zeroes-function)abbreviating-the-product-of-a-range)maximum-trailing-zeros-in-a-cornered-path)Source-backed implementations are provided below for direct study and interview prep.
// Accepted solution for LeetCode #172: Factorial Trailing Zeroes
class Solution {
public int trailingZeroes(int n) {
int ans = 0;
while (n > 0) {
n /= 5;
ans += n;
}
return ans;
}
}
// Accepted solution for LeetCode #172: Factorial Trailing Zeroes
func trailingZeroes(n int) int {
ans := 0
for n > 0 {
n /= 5
ans += n
}
return ans
}
# Accepted solution for LeetCode #172: Factorial Trailing Zeroes
class Solution:
def trailingZeroes(self, n: int) -> int:
ans = 0
while n:
n //= 5
ans += n
return ans
// Accepted solution for LeetCode #172: Factorial Trailing Zeroes
struct Solution;
impl Solution {
fn trailing_zeroes(mut n: i32) -> i32 {
let mut sum = 0;
while n > 0 {
sum += n / 5;
n /= 5;
}
sum
}
}
#[test]
fn test() {
assert_eq!(Solution::trailing_zeroes(3), 0);
assert_eq!(Solution::trailing_zeroes(5), 1);
assert_eq!(Solution::trailing_zeroes(10), 2);
}
// Accepted solution for LeetCode #172: Factorial Trailing Zeroes
function trailingZeroes(n: number): number {
let ans = 0;
while (n > 0) {
n = Math.floor(n / 5);
ans += n;
}
return ans;
}
Use this to step through a reusable interview workflow for this problem.
Simulate the process step by step — multiply n times, check each number up to n, or iterate through all possibilities. Each step is O(1), but doing it n times gives O(n). No extra space needed since we just track running state.
Math problems often have a closed-form or O(log n) solution hidden behind an O(n) simulation. Modular arithmetic, fast exponentiation (repeated squaring), GCD (Euclidean algorithm), and number theory properties can dramatically reduce complexity.
Review these before coding to avoid predictable interview regressions.
Wrong move: Temporary multiplications exceed integer bounds.
Usually fails on: Large inputs wrap around unexpectedly.
Fix: Use wider types, modular arithmetic, or rearranged operations.