Shortest Palindrome

Algorithms
Hard
Meta
67.1K views

Given a string $s$, find the shortest palindrome that can be formed by adding characters in front of it. Use KMP (Knuth-Morris-Pratt) preprocessing to find the longest palindromic prefix.

Why Interviewers Ask This

Meta evaluates candidates on this problem to assess mastery of string manipulation algorithms and the ability to optimize beyond naive brute-force solutions. Interviewers specifically look for your capacity to apply advanced preprocessing techniques like KMP to solve complex pattern-matching problems efficiently, demonstrating strong analytical thinking under time pressure.

How to Answer This Question

1. Clarify requirements immediately: confirm if input is empty or null and define 'shortest palindrome' as adding minimal characters only to the front. 2. Analyze the core constraint: explain that a brute-force check of all prefixes takes O(N^2), which is too slow, necessitating an O(N) approach. 3. Propose the KMP strategy: articulate the insight that finding the longest palindromic prefix of s allows us to reverse the remaining suffix and prepend it. 4. Detail the construction: describe creating a new string by concatenating s, a separator, and the reverse of s (s + '#' + reverse(s)). 5. Explain the algorithm execution: walk through computing the failure function (pi array) for this combined string, noting that the last value in pi represents the length of the longest palindromic prefix. 6. Conclude with implementation: state that you will append the non-palindromic suffix in reverse order to complete the solution, ensuring optimal time and space complexity.

Key Points to Cover

  • Demonstrating knowledge of KMP failure function mechanics rather than just memorizing code
  • Identifying the specific transformation (S + # + reverse(S)) to convert palindrome search into prefix-suffix matching
  • Explicitly stating the time complexity reduction from O(N^2) to O(N)
  • Handling edge cases like empty strings or single characters during the explanation
  • Articulating the logical connection between the last value in the pi array and the palindrome length

Sample Answer

To solve the Shortest Palindrome problem efficiently, I would first clarify that we need to add characters strictly to the front. A naive approach checking every prefix for palindromicity would result in O(N^2) time complexity, which isn't ideal for large inputs typical at Meta. Instead, I propose using the Knuth-Morris-Pratt (KMP) algorithm's preprocessing logic. The key insight is that if we can find the longest palindromic prefix of the original string, say of length L, then the remaining suffix of length N-L must be reversed and added to the front. To find this L efficiently, I construct a new string T by concatenating the original string S, a unique separator character '#', and the reverse of S. For example, if S is 'aacecaaa', reverse(S) is 'aaacecaa', making T 'aacecaaa#aaacecaa'. Next, I compute the KMP failure function (pi array) for T. The value at the very end of this pi array tells us the length of the longest proper prefix of T that is also a suffix of T. Because of our construction, this value corresponds exactly to the length of the longest palindromic prefix of the original string S. Finally, I take the substring of S starting from index L to the end, reverse it, and prepend it to S. This entire process runs in O(N) time and uses O(N) space, providing an optimal solution that demonstrates deep algorithmic understanding.

Common Mistakes to Avoid

  • Attempting to use Manacher's algorithm when the interviewer specifically requested KMP preprocessing
  • Forgetting to include a unique separator character between the string and its reverse, causing false overlaps
  • Implementing the brute-force solution without recognizing the performance constraints of O(N^2)
  • Confusing the direction of the reversal or failing to correctly identify which suffix needs to be prepended

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 71 Meta questions