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

Exams that ask this topic