Reported April 2020
Bloombergdynamic programming

Minimum Insertions to Make a Palindrome

Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live Bloomberg OA. Under 2s to a working solution.
Founder's read

Strip away the wording and this Bloomberg problem, reported in April 2020, is a longest palindromic subsequence question in disguise. The insertion count is just the string length minus that subsequence. If you see "minimum insertions to make a palindrome" in your invite, that's the whole reduction. The hinted pattern says two-pointers, but the real engine is a 2D DP over the string. Length goes up to 2000, so an O(n^2) table is fine. If you blank on the recurrence mid-assessment, StealthCoder sits invisibly on your screen and can hand you the solution while you keep typing.

The problem

Return the minimum number of single-character insertions needed to turn text into a palindrome. Insertions may occur at any positions.

Function
minInsertionsPalindrome(text: String) → int

Examples
Example 1
text = "apple"
return = 3
Three insertions suffice; equivalently, apple has a longest palindromic subsequence of length 2.

Constraints
0 <= text.length <= 2000.
The text contains lowercase English letters.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick: every character that's already part of the longest palindromic subsequence (LPS) needs no partner. Every other character needs one inserted mirror. So the answer is n minus LPS. Compute LPS with dp[i][j] over substring i..j. If text[i] equals text[j], dp[i][j] = dp[i+1][j-1] + 2. Otherwise take the max of dp[i+1][j] and dp[i][j-1]. You can also skip LPS and do it directly: if the ends match, shrink both sides, else 1 + min of shrinking either side. The two-pointer feel comes from that i and j shrinking. The pitfall is greedy thinking, where you only compare ends and insert. That fails on strings like apple. Also handle the empty string, which returns 0. Space can drop to O(n) with a rolling row, but 2000 squared fits fine. If the recurrence slips under pressure, StealthCoder is your hedge during the live OA.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill Minimum Insertions to Make a Palindrome 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 for the candidate who got the OA invite this morning and has 72 hours, not six months.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as minimum insertion steps to make a string palindrome. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass Bloomberg's OA.

Bloomberg reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Minimum Insertions to Make a Palindrome FAQ

What's the trick to Minimum Insertions to Make a Palindrome?+

Reduce it to the longest palindromic subsequence. Answer equals length minus LPS. For apple, LPS is 2 (like pp), so 5 minus 2 gives 3. Once you see that, it's a standard interval DP with two indices shrinking inward.

Is two-pointers really the right pattern here?+

Only loosely. You use left and right indices, but a plain two-pointer greedy scan fails when the ends mismatch, because you have to try both options. You need DP or memoization on (i, j). Treat the pointers as DP state, not a greedy walk.

Will O(n^2) pass for length 2000?+

Yes. The constraint is 2000, so a 2001 by 2001 table is about 4 million cells. That's fine in most languages. You can compress to two rows if you want less memory, but it's not required for this input size.

What edge cases should I test?+

Test the empty string (return 0), a single character (0), an already-palindromic string (0), and all-distinct letters like abcd (3). Also try a string like apple, where the expected answer is 3. These catch most off-by-one bugs in the DP bounds.

How do I prepare for this in 48 hours?+

Write the interval DP from scratch twice. Once as the LPS table, once as the direct insertion recurrence. Make sure you can explain why answer equals n minus LPS. Then run it on apple and a few tiny strings by hand. That's enough for this one.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with Bloomberg.

OA at Bloomberg?
Invisible during screen share
Get it