Data Structures & AlgorithmsModerateTCS NQT · Accenture

Problem: A string is "racecar" and we ask: how many substrings of length 2 or 3 are palindromes (counted by index ranges)?

  1. A. 1
  2. B. 2
  3. C. 0
  4. D. 3

Correct answer: A. 1

Test each window of length 2 and 3 for palindromicity. Length-2 palindromes require equal neighbours, and there are none in 'racecar'; the count then comes only from length 3.

More Coding Interview Problems questions

Exams that ask this topic