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?
  1. A. 109
  2. B. 108
  3. C. 100
  4. 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

Exams that ask this topic