Interleaving String
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Bloomberg reportedly served Interleaving String in October 2019, and the constraints are the whole story. With s1 and s2 up to 100 characters each, trying every way to split s3 between them means up to 2^200 paths, and brute force dies fast. The pattern is dynamic programming over two pointers, and it's a classic. If you've seen it, you write it in ten minutes. If you haven't, the table is easy to miss under pressure. StealthCoder is the safety net on the live OA if your mind goes blank, but the trick below is short enough to carry in your head.
The problem
Given strings s1, s2, and s3, return whether s3 can be formed by interleaving s1 and s2. An interleaving uses every character from both source strings exactly once while preserving the left-to-right order within each source. Characters chosen from the two sources may alternate in groups of any positive length. Function isInterleave(s1: String, s2: String, s3: String) → boolean Examples Example 1 s1 = "aabcc" s2 = "dbbca" s3 = "aadbbcbcac" return = true The target can choose characters from both source strings while retaining each source's internal order. Example 2 s1 = "aabcc" s2 = "dbbca" s3 = "aadbbbaccc" return = false Every possible choice eventually needs to reverse or skip a character from one source, so the target is not an interleaving. Example 3 s1 = "" s2 = "" s3 = "" return = true The empty target uses every character from both empty source strings. Constraints 0 <= s1.length, s2.length <= 100. 0 <= s3.length <= 200. All three strings contain only lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
First check: if len(s1) + len(s2) != len(s3), return false immediately. Then define dp[i][j] as true if the first i chars of s1 and first j chars of s2 can interleave into the first i+j chars of s3. dp[0][0] is true. Fill each cell from two sources: dp[i-1][j] is true and s1[i-1] equals s3[i+j-1], or dp[i][j-1] is true and s2[j-1] equals s3[i+j-1]. The answer is dp[m][n]. That's O(m*n) time, about 10,000 cells, trivial. The common pitfall is greedy matching: when both s1 and s2 match the current s3 char, picking one can be wrong, so you need DP or memoized recursion. Another miss is forgetting the first row and column, where one string is empty. You can compress to a single row of size n+1. If you freeze on the recurrence during the live OA, StealthCoder can hand you the working table as a hedge.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill Interleaving 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. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as interleaving string. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Interleaving String FAQ
What's the trick to Interleaving String?+
Treat it as a grid where dp[i][j] means s1's first i chars and s2's first j chars can build s3's first i+j chars. Each cell comes from the cell above or the cell to the left, depending on which character matches. Greedy fails because both strings can match the same character.
How hard is this one really?+
Medium. The recurrence is small once you see the grid, but people stumble on the index math, s3[i+j-1], and the base cases. It's far easier than most hard DP problems. If you've done edit distance or longest common subsequence, this feels familiar.
Why can't I just use two pointers greedily?+
If s1 and s2 both have the current s3 character at their pointers, you can't know which to consume. A wrong choice only shows up later. Example: s1 = "aab", s2 = "aac". Greedy needs backtracking, and DP is the clean way to explore both branches without exponential blowup.
What edge cases should I test before submitting?+
Test all three strings empty (true), mismatched lengths (false early), one source empty so s3 must equal the other, and a case where both sources start with the same letter. Also check that you handle the first row and column of the table without indexing out of bounds.
How do I prepare for this in 48 hours?+
Write the 2D DP version from memory twice, then reduce it to a 1D array. Run it on the three given examples by hand. Spend the rest of the time on neighbors like longest common subsequence and edit distance, since the grid-fill reasoning carries over.