Split a Caption Into Pieces That Are All Palindromes in Every Way

Last updated: November 29, 2026

Quick Overview

Given a caption string, return every way to partition it into contiguous pieces such that each piece reads the same forward and backward.

Meta
Coding & Algorithms
Software Engineer
Meta
November 29, 2026
Software Engineer
Onsite
Coding & Algorithms
Medium

0

0

0 solved


Given a caption string, return every way to partition it into contiguous pieces such that each piece reads the same forward and backward.

This is a Meta onsite backtracking question that rewards combining a DP precomputation with the search. The interviewer checks that you avoid recomputing palindrome checks in the inner loop and can explain the exponential output bound.

What the Interviewer Expects
  • Precompute a palindrome table in O(n^2)
  • Backtrack over end positions from the current start
  • Only recurse when the piece is a palindrome
  • Copy the partition at the leaf
  • Explain why output can be 2^(n-1) in size
Key Topics to Cover
Backtracking
Dynamic programming
Palindromes
Strings
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
  • How would you return only the partition with the fewest pieces?
  • How would you count partitions without listing them?
  • How would you do this on a stream where characters keep arriving?
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
  • Caption length up to 16.
  • Return every palindromic partition, any order.
  • A single character is always a palindrome....
High-Level Design

First fill is_pal[i][j] from the bottom up: s[i..j] is a palindrome if the end characters match and the inside is a palindrome (or the span is at most 2 long). Then backtrack: from index start, ...


Submit Your Answer
Markdown supported