Data Structures & AlgorithmsHardProduct Company SDE · CP practice
Problem: How many integers in [1, 10^4] are divisible by 3 and have an odd number of digits? Run the code.
Problem: How many integers in [1, 10^4] are divisible by 3 and have an odd number of digits? Run the code.
print(sum(1 for x in range(1, 10001) if x % 3 == 0 and len(str(x)) % 2 == 1))What is printed?
- A. 303
- B. 3333
- C. 3030
- D. 300
Correct answer: A. 303
Odd digit counts in range are 1, 3 and 5 (10000 itself is not a multiple of 3): 3 + 300 + 0 = 303. 3333 counts every multiple of 3, 3030 counts the even-length ones, and 300 forgets the single digits 3, 6, 9.
More Coding Interview Problems questions
- Problem: What is the number of distinct subsets sums obtainable from {2, 3, 7} (including the empty subset)? Run the code. python from…
- Problem: What is the largest prime factor of 2^20 - 1? Run the code. python n = 2 20 - 1 f = 2 last = 1 while f f <= n: while n % f == 0…
- Problem: How many numbers in [1, 500] are not divisible by 2, 3 or 5? Run the code. python print(sum(1 for x in range(1, 501) if x % 2 and…
- Problem: What is the number of inversions in the array [1, 3, 2, 5, 4, 6]? Run the code. python a = [1, 3, 2, 5, 4, 6] print(sum(1 for i…
- Problem: What is the maximum sum of a subarray with at most one element deleted from [1, -2, 3, -4, 5]? Run the code. python a = [1, -2…
- Problem: How many three-letter words over {a, b, c} contain every letter at least once? Run the code. python from itertools import product…