Data Structures & AlgorithmsModerateProduct Company SDE · CP practice
Problem: How many 3-digit numbers are palindromes?
Problem: How many 3-digit numbers are palindromes?
print(sum(1 for x in range(100, 1000) if str(x) == str(x)[::-1]))What is printed?
- A. 90
- B. 100
- C. 81
- D. 89
Correct answer: A. 90
A 3-digit palindrome is fixed by its first digit (1-9) and middle digit (0-9): 9 × 10 = 90. Allowing a leading 0 gives 100, forbidding a 0 in the middle gives 81, and range(100, 999) misses 999 (89).
More Coding Interview Problems questions
- Problem: How many integers in [1, 200] are divisible by 2, 3 or 5? python print(sum(1 for x in range(1, 201) if x % 2 == 0 or x % 3 == 0…
- Problem: How many subsets of {1, 2, 3, 4, 5, 6} have sum exactly 10? python print(sum(1 for m in range(1 << 6) if sum(i + 1 for i in…
- Problem: What is the sum of the decimal digits of 9^9? python print(sum(int(c) for c in str(9 9))) What is printed?
- Problem: What is the length of the longest increasing subsequence of [3, 10, 2, 1, 20]? python import bisect a = [3, 10, 2, 1, 20] tails =…
- Problem: How many distinct values does floor(30 / i) take for i = 1 to 30? python print(len({30 // i for i in range(1, 31)})) What is…
- Problem: How many steps does Euclid's algorithm take on (2024, 748) before the remainder becomes 0? python a, b = 2024, 748 steps = 0…