ProgrammingVery hardTCS NQT · Accenture

The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of length 3 (windows start at 0 to n-3). What is printed?

The palindromic substrings of length exactly 3 in the string "abacaba" are counted by the procedure below, which checks each window of length 3 (windows start at 0 to n-3). What is printed?
count = 0
for i = 0 to n - 3
    if s[i] == s[i+2] then count = count + 1
end for
Print count
  1. A. 0
  2. B. 3
  3. C. 5
  4. D. 7

Correct answer: B. 3

The windows aba, bac, aca, cab and aba are checked: the first, third and last are palindromes, so the count is 3. Testing adjacent equal letters finds none, the window count is 5, and the length of the word is 7.

More Pseudocode and Code Tracing questions

Exams that ask this topic