Data Structures & AlgorithmsModerateTCS NQT · number puzzle

Problem: How many 4-digit numbers (from 1000 to 9999) are palindromes? Run the code.

Problem: How many 4-digit numbers (from 1000 to 9999) are palindromes? Run the code.
print(sum(1 for x in range(1000, 10000) if str(x) == str(x)[::-1]))
What is printed?
  1. A. 90
  2. B. 100
  3. C. 99
  4. D. 80

Correct answer: A. 90

The first digit matches the last (9 choices for the first digit) and the middle two digits must mirror each other (10 choices), giving 90.

More Coding Interview Problems questions

Exams that ask this topic