TikTok Interview Problem #6 — Shortest Palindrome by Adding Characters to the Front|Interview Prep

45 Views
No Comments

You are given a string s.
You can convert s to a palindrome by adding characters in front of it.
Return the shortest palindrome you can form by performing this transformation.

Input:
s = "aacecaaa"

Output:
"aaacecaaa"

The task is to create the shortest palindrome by adding characters only to the front of the given string.

Main idea:

  1. Find the longest palindrome prefix of the string.
  2. Reverse the remaining suffix and prepend it to the front.
  3. The result is the shortest palindrome that can be formed.

Optimal solutions use KMP or two-pointer matching to find the longest palindromic prefix in O(n) time.

The VOprep team has long accompanied candidates through various major company OAs and VOs, including Tiktok, Google, Amazon, Citadel, SIG, providing real-time voice assistance, remote practice, and interview pacing reminders to help you stay smooth during critical moments. If you are preparing for Tiktok or similar engineering-focused companies, you can check out our customized support plans—from coding interviews to system design, we offer full guidance to help you succeed.

END
 0