Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many integers in [1, 10000] are divisible by 9 but not by 27? Run the code.
Problem: How many integers in [1, 10000] are divisible by 9 but not by 27? Run the code.
print(sum(1 for x in range(1, 10001) if x % 9 == 0 and x % 27 != 0))What is printed?
- A. 1111
- B. 741
- C. 370
- D. 556
Correct answer: B. 741
Multiples of 9: 1111; of 27: 370; difference 741. 1111 forgets to exclude 27, 370 is the excluded set, and subtracting multiples of 18 gives 556.
More Coding Interview Problems questions
- Problem: How many trailing zero bits (factors of 2) does 1000! have? Run the code. python n = 1000 c = 0 p = 2 while p <= n: c += n // p p…
- 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…