MEDIUMasked at 2 companies

Factorial Trailing Zeroes

A medium-tier problem at 45% community acceptance, tagged with Math. Reported in interviews at Google and 1 others.

Founder's read

Factorial Trailing Zeroes is a medium-difficulty math problem that trips up candidates who dive straight into computing the factorial. Google and TCS ask it regularly. You can't just compute N! and count zeros, the numbers get too large fast. The trick is understanding where trailing zeros come from: they're products of 2 and 5 in the prime factorization, and 5s are always the bottleneck. If you hit this live and blank on the pattern, StealthCoder surfaces the counting approach invisibly during your assessment.

Companies asking
2
Difficulty
MEDIUM
Acceptance
45%

Companies that ask "Factorial Trailing Zeroes"

If this hits your live OA

Factorial Trailing Zeroes is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share.

Get StealthCoder
What this means

The intuition most candidates miss: you don't calculate the factorial itself. Instead, count how many times 5 divides N!, since every trailing zero requires a factor of 2 and a factor of 5, and 2s always outnumber 5s. The formula counts 5s in N!, 5^2s (25s contribute an extra 5), 5^3s, and so on. floor(N/5) + floor(N/25) + floor(N/125) +... solves it in linear time. The gotcha is forgetting higher powers of 5. If you're stuck during the OA and haven't drilled this pattern, StealthCoder handles it without the proctor knowing.

Pattern tags

The honest play

You know the problem. Make sure you actually pass it.

Factorial Trailing Zeroes recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer at a top-10 tech company who can solve these problems cold but didn't want to trust himself in a 90-minute screen share. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Factorial Trailing Zeroes interview FAQ

Why can't I just compute the factorial and count zeros?+

Factorial grows explosively. 21! exceeds a 64-bit integer. You need a mathematical insight, not brute force. The pattern is recognizing that trailing zeros come from factor pairs of 2 and 5, and counting 5s directly in the prime factorization.

Is this still asked at Google?+

Yes. It appears in Google's reported questions for this problem. It's a litmus test for whether you think mathematically or just code. The acceptance rate is around 45%, so half the candidates get it wrong under pressure.

What's the most common mistake?+

Counting only floor(N/5) and forgetting higher powers. N=25 gives 6 trailing zeros, not 5. You must add floor(N/25) + floor(N/125) +... to account for numbers like 25, 50, 125 that contribute multiple factors of 5.

How does this relate to prime factorization?+

Trailing zeros are created by 10s, which factor as 2 × 5. Any factorial has way more 2s than 5s, so 5 is the limiting factor. Count 5s in the prime factorization of N!, not the full factorial.

Can I solve this without understanding the math trick?+

Not really. Brute force and digit-counting fail on large inputs. You need the counting formula. If you haven't seen the pattern before, it's exactly the kind of problem where a live safety net like StealthCoder saves you.

Want the actual problem statement? View "Factorial Trailing Zeroes" on LeetCode →

Frequency and company-tag data sourced from public community-maintained interview-report repos. Problem, description, and trademark © LeetCode. StealthCoder is not affiliated with LeetCode.