Data Structures & AlgorithmsHardGATE CS · Infosys

What is the length of the longest prefix of the string ababbaba that is itself a palindrome?

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

Correct answer: D. 8

Check prefixes against their reverse from the longest: 'ababbaba' reversed is 'ababbaba', so the whole string (8) is a palindrome. Stopping at the first palindromic prefix after 'a' gives 3 (aba), 1 is the trivial prefix and 7 assumes the full string cannot qualify.

More Arrays, Strings, Linked Lists, Stacks, Queues and Hashing questions

Exams that ask this topic