Google Interview Question
1,230 Interview Reviews |
Back to all Google Interview Questions & Reviews
Interview questions and reviews posted anonymously by interview candidates
Interview Question for Software Engineer at Google:
Write a function in C/C++ that returns the number of zeros contained in the factorial of the number that is passed to it.
Helpful Question?
Yes |
No
Inappropriate?
Answers & Comments (5)
Helpful Answer?
Yes |
No
Inappropriate?
0 of 2 people found this helpful
int func(int number_to_get_factorial_of) {
return 0;
}
Helpful Answer?
Yes |
No
Inappropriate?
int func(int x) {
int factorial = 1;
int zeros = 0;
while (x) {
factorial *= x--;
}
while (factorial) {
zeros += (factorial % 10 == 0);
factorial /= 10;
}
return zeros;
}
Helpful Answer?
Yes |
No
Inappropriate?
1 of 1 people found this helpful
George is right that you just need the number of 5's in the prime factorization (there will always be lots of 2's), but dividing n by 5 does not quite give that to you.
You need n/5 + n/25 + n/125 + ..... In time the terms become 0 and you can stop.
Helpful Answer?
Yes |
No
Inappropriate?
To comment on this
question,
Sign In with Facebook or
Sign Up
1 of 3 people found this helpful
by George:
def number_of_zeros( n ):
return n / 5