Preimage Size of Factorial Zeroes Function

HardMathbinary search

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

Let f(x) be the number of trailing zeros in x! (with 0! = 1). For example f(3) = 0 because 3! = 6, and f(11) = 2 because 11! = 39916800. Given a non-negative integer k, return how many non-negative integers x satisfy f(x) = k. The answer is always either 0 or 5. k can be up to 10^9, so search for x rather than computing factorials, and use 64-bit integers for the search range.

Examples

Input: k = 0
Output: 5
0!, 1!, 2!, 3! and 4! all end with no zeros.
Input: k = 5
Output: 0
24! ends with 4 zeros and 25! with 6, so nothing has exactly 5.
Input: k = 3
Output: 5
x = 15, 16, 17, 18, 19 all have exactly 3 trailing zeros in their factorials.

Constraints

  • 0 <= k <= 10^9

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More binary search problems