Ways to Split a String
Reported by candidates from Google's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Google's February OA included a string-splitting problem that looks deceptively simple on first read. You're given a string and need to find valid ways to split it according to some constraint, usually around palindromes or numeric patterns. The trap is treating it as a greedy problem when it's actually about exhaustive enumeration. StealthCoder will catch the pattern if you blank on the recursive structure mid-interview.
Pattern and pitfall
The core pattern here is backtracking. You build splits recursively, checking validity at each step, and exploring all paths. The trick candidates miss is that greedy won't work because an invalid early split might block a valid solution later. The common pitfall is computing validity inefficiently on every candidate split instead of precomputing or memoizing. If the constraint involves palindromes, you'll want a 2D DP table up front to know which substrings are valid in constant time. Backtrack with that table as your lookup, prune branches early, and collect results. StealthCoder's role is to remind you of the structure if you freeze on whether to use DP or recursion.
Memorize the pattern. If you can't, run StealthCoder. The proctor sees the IDE. They don't see what's behind it.
You can drill Ways to Split a String cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as palindrome partitioning. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Google's OA.
Google reuses patterns across OAs. Made by an engineer who treats the OA as theater. If yours is tonight, you don't have time to grind. You have time to hedge. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Ways to Split a String FAQ
Is this a DP problem or backtracking?+
Both. Use DP as a preprocessing step to mark valid substrings (e.g., palindromes). Then backtrack to enumerate all valid splits using that DP table for O(1) lookups. This hybrid approach avoids redundant validity checks.
What's the main gotcha Google tests here?+
Candidates try greedy first and fail silently. The problem requires exploring all branches because an early invalid choice doesn't mean the string is unsplittable. Exhaustive search is the correct approach, not optimization.
How do I precompute validity efficiently?+
Build a 2D boolean table: dp[i][j] = true if substring s[i:j+1] is valid. Fill it bottom-up (expanding from single chars). Then in backtracking, check dp[start][end] in O(1) instead of recomputing validity each time.
What's the time complexity I should expect?+
Worst case is exponential in the number of valid splits (think all substrings are valid). That's inherent to the problem, not a sign of a wrong approach. Google expects you to recognize this and accept it.
Can I solve this in 30 minutes under pressure?+
Yes, if you recognize backtracking immediately. Write the recursive helper, build your validity table, prune early. Coding it is straightforward once the structure clicks. That's why this shows up on OAs.