Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: What is the number of integers in [1, 1000] whose digits are all odd? Run the code.
Problem: What is the number of integers in [1, 1000] whose digits are all odd? Run the code.
print(sum(1 for x in range(1, 1001) if all(int(c) % 2 == 1 for c in str(x))))What is printed?
- A. 125
- B. 30
- C. 124
- D. 155
Correct answer: D. 155
Odd-digit numbers have 1, 2 or 3 digits from {1,3,5,7,9}: 5 + 25 + 125 = 155. 125 counts only 3-digit ones, 30 only 1- and 2-digit ones, and 124 counts all-even-digit numbers.
More Coding Interview Problems questions
- Problem: What is the length of the longest subarray of [5, 1, 4, 2, 3, 6] whose elements can be sorted to form consecutive integers? Run…
- Problem: How many integers from 1 to 1000 are not divisible by any of 2, 3, 5 and 7? Run the code. python print(sum(1 for x in range(1…
- Problem: What is the maximum sum of a subsequence (not necessarily contiguous) of [4, -2, 5, -1, 3] with no two chosen elements adjacent?…
- Problem: How many ways are there to write 12 as a sum of distinct positive integers (partitions into distinct parts)? Run the DP. python…
- Problem: What is the number of trailing zeros of 25 40 125? Run the code. python n = 25 40 125 c = 0 while n % 10 == 0: n //= 10 c += 1…
- Problem: How many integer pairs (x, y) with 1 <= x <= 10 and 1 <= y <= 10 satisfy x y being a perfect cube? Run the code. python…