Data Structures & AlgorithmsModerateTCS NQT Digital · Number puzzle

Problem: How many integers x with 1 <= x <= 100 have no two adjacent equal decimal digits? Use the rule that a one-digit number always qualifies.

Problem: How many integers x with 1 <= x <= 100 have no two adjacent equal decimal digits? Use the rule that a one-digit number always qualifies.
cnt = 0
for x in range(1, 101):
    s = str(x)
    if all(s[i] != s[i + 1] for i in range(len(s) - 1)):
        cnt += 1
print(cnt)
What is printed?
  1. A. 81
  2. B. 90
  3. C. 99
  4. D. 100

Correct answer: B. 90

All 9 one-digit numbers qualify, and 81 two-digit numbers have distinct digits (excluding 11, 22, ..., 99). The number 100 contains '00' and is excluded, so the total is 90.

More Coding Interview Problems questions

Exams that ask this topic