Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: What is the number of trailing zeros of 1000!?
Problem: What is the number of trailing zeros of 1000!?
n = 1000
c = 0
p = 5
while p <= n:
c += n // p
p *= 5
print(c)What is printed?- A. 200
- B. 240
- C. 250
- D. 249
Correct answer: D. 249
Legendre's formula counts factors of 5 in 1000!: 200 + 40 + 8 + 1 = 249, and factors of 2 are always plentiful. Stopping after 1000 // 5 gives 200, after the 25-term gives 240, and the shortcut n / 4 = 250 overcounts because the floors lose fractions.
More Coding Interview Problems questions
- Problem: How many 1-bits are set in 2^20 - 1? python print(bin(2 20 - 1).count('1')) What is printed?
- Problem: What is the maximum sum of a contiguous subarray of exactly length 3 in [5, -2, 7, 1, 0, 4]? python a = [5, -2, 7, 1, 0, 4]…
- Problem: How many subsets of {1, 2, ..., 10} contain no two consecutive integers? python from itertools import combinations print(sum(1…
- Problem: How many integers in [1, 1000] are perfect squares or perfect cubes? python print(sum(1 for x in range(1, 1001) if any(k k == x…
- Problem: Greedy graph colouring assigns colours in vertex order 0, 1, 2, 3, 4, 5, giving each vertex the smallest colour (starting at 0)…
- Problem: In the undirected graph with edges 0-1, 0-2, 1-2, 1-3, 2-3, 3-4, how many simple paths (no repeated vertex) go from vertex 0 to…