Data Structures & AlgorithmsHardTCS CodeVita · Microsoft SDE OA
How many trailing zeros does 100! have in base 10? Use Legendre's formula with powers of 5, counted by running the loop over p = 5, 25, 125, ...
How many trailing zeros does 100! have in base 10? Use Legendre's formula with powers of 5, counted by running the loop over p = 5, 25, 125, ...
n=100; c=0; p=5
while p<=n:
c+=n//p; p*=5
print(c)What is printed?- A. 97
- B. 26
- C. 24
- D. 23
Correct answer: C. 24
Trailing zeros come from factors of 10 = 2·5, and 2s are plentiful, so the count is the power of 5 in 100!: 20 + 4 = 24, including the extra 5s in 25 and 75. Adding a term for 50 is not a power of 5, counting the 2s gives a different number, and subtracting one is an off-by-one.
More Coding Interview Problems questions
- The string s = "abcbdab" needs characters deleted to become a palindrome. What is the minimum number of deletions? Use n minus the longest…
- A 4×4 grid has rows "..#.", ".##.", "##.#", "#...". Entering a cell costs 1 if it is '#' and 0 otherwise, and you may move in four…
- Array c = [1, 2, 3, 0, 3, 3, 0, 6]. In how many ways can it be split into three non-empty contiguous parts whose sums are all equal? Run…
- Array d = [1, 2, 1, 3, 4, 3, 5]. Count the contiguous subarrays containing exactly three distinct values. Run the brute-force code. python…
- Houses have values e = [2, 7, 9, 3, 1, 8, 4, 6] and a robber cannot take two adjacent houses. Find the maximum sum that can be taken…
- Count the subarrays of v = [4, 6, 2, 1, 7, 3, 5, 1] whose XOR equals 0, considering only subarrays of length at least 3. Brute-force all…