Data Structures & AlgorithmsModerateProduct Company SDE · CP practice

Problem: How many palindromes of length 4 can be formed over the alphabet {a, b}? Run the code.

Problem: How many palindromes of length 4 can be formed over the alphabet {a, b}? Run the code.
from itertools import product
print(sum(1 for t in product('ab', repeat=4) if t == t[::-1]))
What is printed?
  1. A. 5
  2. B. 4
  3. C. 3
  4. D. 6

Correct answer: B. 4

A length-4 palindrome is determined by its first two letters, giving 2^2 choices.

More Coding Interview Problems questions

Exams that ask this topic