Count Trailing Zeros in the Factorial of a Reel Count

Last updated: November 29, 2026

Quick Overview

Given an integer n up to 10^18, return how many trailing zeros n! has in base 10, without computing n!.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Easy

0

0

0 solved


Given an integer n up to 10^18, return how many trailing zeros n! has in base 10, without computing n!.

A short math question Meta interviewers use to see whether you reason about prime factors instead of brute force. The follow-ups probe whether the reasoning generalizes to other bases.

What the Interviewer Expects
  • Explain that trailing zeros come from factors of 10 = 2 x 5
  • Argue that 5s are the limiting factor
  • Count multiples of 5, 25, 125, and so on (Legendre's formula)
  • Give an O(log n) solution
  • Generalize to other bases
Key Topics to Cover
Number theory
Legendre's formula
Prime factorization
Logarithmic time
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • How many trailing zeros does n! have in base 12?
  • Find the smallest n whose factorial has at least k trailing zeros.
  • Can any count of zeros be skipped as n increases?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements

Input: integer n with 0 <= n <= 10^18. Output: number of trailing zeros of n! in decimal. Computing n! is impossible at this scale, so the answer must come from counting factors....

High-Level Design

Each trailing zero is a factor of 10 = 2 x 5. Among 1..n there are always at least as many factors of 2 as of 5, so the answer equals the exponent of 5 in n!. Legendre's formula gives that exponent as...


Submit Your Answer
Markdown supported