Data Structures & AlgorithmsModerateTCS NQT · Infosys

Problem: The string "racecar" is compared with its reverse position by position. How many positions have matching characters?

  1. A. 3
  2. B. 6
  3. C. 1
  4. D. 7

Correct answer: D. 7

"racecar" is a palindrome, so s[i] = s[6 − i] at all 7 positions. Counting only mirrored pairs gives 3, leaving out the middle gives 6, and 1 counts only the centre.

More Coding Interview Problems questions

Exams that ask this topic