Skip to content
HardStringsAI interview only

Longest Chunked Palindrome Decomposition

Asked atgoogleamazonmicrosoft

01 · Problem

You are given a string text. Cut it into k non-empty consecutive chunks c1, c2, ..., ck so that:

  • concatenating the chunks in order gives back text, and
  • the chunk sequence reads the same from both ends: c_i == c_(k-i+1) for every 1 <= i <= k.

Return the largest possible k. A single chunk (k = 1, the whole string) always satisfies the rule, so the answer is at least 1. When k is odd, the middle chunk only has to equal itself.

02 · Examples

Example 01
Input
text = "volvo"
Output
3

Split as "vo" | "l" | "vo": the first and last chunks match and the middle chunk pairs with itself.

Example 02
Input
text = "abcabc"
Output
2

Split as "abc" | "abc". Any finer split fails because "a" does not equal "c".

Example 03
Input
text = "elephant"
Output
1

No prefix of length 1-4 ("e", "el", "ele", "elep") equals the suffix of the same length ("t", "nt", "ant", "hant"), so the whole string stays as one chunk.

03 · Constraints

  • 011 <= text.length <= 1000
  • 02text consists only of lowercase English letters
  • 03Every chunk must be non-empty, so the answer k satisfies 1 <= k <= text.length

04 · Optimal complexity

Time
O(n)
Space
O(1)
05 · Two ways to work on it

Practice it alone or rehearse it as an interview.

Practice Mode gives you an editor and test runs, nothing else. AI Interview Mode puts a voice interviewer on the other side, adds a clock, and ends with a scored summary of the round.