ProgrammingHardInfosys · Cognizant

For the string s = "forgeeksskeegfor", the procedure below expands around each centre to find the length of the longest palindromic substring. What is printed?

For the string s = "forgeeksskeegfor", the procedure below expands around each centre to find the length of the longest palindromic substring. What is printed?
best = 1
for each centre c (between or on characters)
    expand while the left and right characters match
    best = max(best, length of the largest palindrome found)
end for
Print best
  1. A. 1
  2. B. 9
  3. C. 10
  4. D. 8

Correct answer: C. 10

The palindrome geeksskeeg has length 10, found by expanding around the double-letter centre ss. Centres on single characters only give 1, subtracting one from the answer gives 9, and the length of the outer part gives 8.

More Pseudocode and Code Tracing questions

Exams that ask this topic