Split a Search Query Into Palindromic Pieces Every Possible Way

Last updated: November 29, 2026

Quick Overview

Given a lowercase string, return every way to cut it into contiguous pieces such that each piece reads the same forwards and backwards.

Google
Coding & Algorithms
Software Engineer
Google
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

0

0 solved


Given a lowercase string, return every way to cut it into contiguous pieces such that each piece reads the same forwards and backwards.

A Google technical-screen problem that rewards combining backtracking with a precomputed table. The interviewer checks that you avoid re-testing palindromes inside the search by building an O(n^2) DP table first.

What the Interviewer Expects
  • Backtracks over the end index of the next piece
  • Precomputes is_pal[i][j] in O(n^2) so each check is O(1)
  • Explains the worst case of 2^(n-1) partitions for strings like aaaa
  • Separates the enumeration from the minimum-cut follow-up
  • Traces a small example
Key Topics to Cover
Backtracking
Dynamic programming
Palindromes
String partitioning
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
  • Return only the minimum number of cuts. What changes?
  • How would you count partitions without listing them?
  • How would you restrict pieces to dictionary words that are also palindromes?
Sharpen Your Skills on Codemia

Practice similar problems with our interactive workspace, get AI feedback, and track your progress.

Practice DSA Problems
Sample Answer
Requirements
  • Input: string s of length n (up to about 16 for full enumeration).
  • Output: all lists of pieces whose concatenation is s and where every piece is a palindrome.
  • Example: "aab" gives [a, a, b] ...
High-Level Design

Step 1, precompute: pal[i][j] is True when s[i..j] is a palindrome. Fill it by increasing length: pal[i][j] = s[i] == s[j] and (j - i < 2 or pal[i+1][j-1]). That is O(n^2) time and space.

Step ...


Submit Your Answer
Markdown supported