Data Structures & AlgorithmsVery hardProduct Company SDE · CP practice

Problem: How many palindromic numbers below 10^4 are divisible by 11? Run the code.

Problem: How many palindromic numbers below 10^4 are divisible by 11? Run the code.
print(sum(1 for x in range(1, 10 ** 4) if str(x) == str(x)[::-1] and x % 11 == 0))
What is printed?
  1. A. 107
  2. B. 198
  3. C. 99
  4. D. 17

Correct answer: A. 107

All 99 palindromes with an even number of digits (11, 22, …, 9999) are multiples of 11, and 8 odd-length ones (e.g. 121, 242, 363) add to 107. 198 counts every palindrome, 99 forgets the 3-digit ones, and 17 stops below 1000.

More Coding Interview Problems questions

Exams that ask this topic