Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code.
Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code.
n = 1000
c = 0
p = 2
while p <= n:
c += n // p
p *= 2
print(c)What is printed?- A. 500
- B. 249
- C. 994
- D. 9
Correct answer: C. 994
Legendre: 500 + 250 + 125 + 62 + 31 + 15 + 7 + 3 + 1 = 994 (also 1000 − popcount(1000)). Counting only even numbers gives 500, 249 is the count of factors of 5 (decimal zeros), and 9 counts powers of 2 up to 1000.
More Coding Interview Problems questions
- Problem: How many distinct values does (i j) mod 7 take for 1 <= i, j <= 7? Run the code. python print(len({(i j) % 7 for i in range(1, 8)…
- Problem: What is the maximum sum of non-adjacent elements of the circular array [3, 2, 7, 10]? Run the code. python def lin(a): inc = exc…
- Problem: How many palindromes of length 4 can be formed over the alphabet {a, b}? Run the code. python from itertools import product…
- Problem: In how many ways can you climb 12 stairs taking 1, 2 or 3 steps at a time (order matters)? Run the DP. python ways = [1] + [0] 12…
- Problem: What is the length of the longest subarray of [1, 2, -3, 3, -1, -2] whose sum is zero? Run the prefix-sum code. python a = [1, 2…
- Problem: How many integers in [1, 100] are sums of two positive cubes? Run the code. python print(len({a 3 + b 3 for a in range(1, 5) for…