FastPrepTrailing Zeros of n Factorial
Problems/Trailing Zeros of n Factorial

Trailing Zeros of n Factorial

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: This factorial trailing-zeroes task, named in the report only by a link, was one of three coding questions in an Adobe first round. The candidate had 45 minutes for the three questions, which came after an intro and project talk, and needed about 5 minutes more.

The report gives no limits or examples. Here n is from 0 to 10^9, so the factorial itself cannot be computed, and the examples are our own.

The judged task matches the report at about 95%.

Given an integer n, return how many zeros n! ends with, where n! = 1 * 2 * ... * n and 0! = 1.

For example, 10! = 3628800 ends with 2 zeros. Do not compute n! itself: for large n it has billions of digits.

Function

factorialTrailingZeros(n: int) → int

Examples

Example 1

n = 25return = 6

Among 1 to 25, the multiples of 5 are 5, 10, 15, 20 and 25, and 25 is 5 times 5, so there are 6 fives in all. There are plenty of twos to pair with them, so 25! ends in 6 zeros.

Example 2

n = 4return = 0

4! = 24 has no factor 5, so it ends in no zeros.

Example 3

n = 100return = 24

There are 20 multiples of 5 up to 100 and 4 multiples of 25, each giving one extra five: 20 + 4 = 24 zeros.

Constraints

  • 0 <= n <= 10^9

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int factorialTrailingZeros(int n) {
    // Write your code here.
}
Checking account…