Data Structures & AlgorithmsModerateTCS NQT · number puzzle
Problem: How many decimal palindromes lie in [1, 1000]? Run the code.
Problem: How many decimal palindromes lie in [1, 1000]? Run the code.
print(sum(1 for x in range(1, 1001) if str(x) == str(x)[::-1]))What is printed?
- A. 109
- B. 108
- C. 100
- D. 90
Correct answer: B. 108
Palindromes are 9 one-digit, 9 two-digit and 90 three-digit numbers, for 108. The number 1000 is not a palindrome.
More Coding Interview Problems questions
- Problem: What is the sum of the squares of the odd numbers from 1 to 10? Run the code. python print(sum(x x for x in range(1, 11) if x % 2…
- Problem: How many iterations does Euclid's algorithm take for gcd(1071, 462)? Count the loop passes of the code. python a, b = 1071, 462…
- Problem: From the left, how many buildings of heights [3, 1, 4, 2, 5] are visible (a building is visible if it is strictly taller than all…
- Problem: For the sorted array [1, 2, 2, 2, 5, 7], how many values are at most 2? Run the code. python import bisect a = [1, 2, 2, 2, 5, 7]…
- Problem: How many strings of length 3 over the alphabet {a, b, c} have no two equal adjacent characters? Run the code. python from…
- Problem: How many divisors does 720 have? Run the code. python print(sum(1 for d in range(1, 721) if 720 % d == 0)) What is printed?