Longest Palindromic Substring

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

The edge case that kills the naive answer here is even-length palindromes like "bb" in "cbbd". Bloomberg reported this one in May 2018, and the twist is the linear time requirement with n up to 200000. The classic DP table is O(n^2) and won't survive. Neither will expand-around-center in the worst case. If you have the OA in a day or two, you need to know the real tool before you open the editor. StealthCoder sits invisibly on your screen as a safety net if the algorithm slips your mind mid-assessment.

The problem

Given a non-empty string s, return its longest contiguous palindromic substring.
A single character is a palindrome. If several longest answers exist, return the leftmost one. Your solution must use linear time.

Function
longestPalindrome(s: String) → String

Examples
Example 1
s = "babad"
return = "bab"
Both bab and aba have maximum length three; bab starts first.
Example 2
s = "cbbd"
return = "bb"
The longest palindromic substring is bb.

Constraints
1 <= s.length <= 200000
s contains lowercase English letters.
Your algorithm must run in O(n) time.
If several longest palindromic substrings exist, return the leftmost one.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The hinted pattern is dynamic programming, but the O(n) constraint rules out the textbook table. The tool is Manacher's algorithm. Insert a separator between every character (and at the ends) so odd and even palindromes both become odd-length. Then keep a center and right boundary, and mirror radii across the center to skip redundant comparisons. Each character gets expanded only when it passes the current right edge, which gives linear time. The pitfalls are mapping the transformed index back to the original string, and the tie rule. Use a strict greater-than when updating the best, so the leftmost winner stays. Start index is (center - radius) / 2. If you blank on the mirror logic during the live OA, StealthCoder is the hedge that gives you the working version without anyone seeing it.

If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.

If this hits your live OA

You can drill Longest Palindromic Substring 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 would have shipped this the night before his JPMorgan OA if he'd had it.

Get StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as longest palindromic substring. 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. Built by an Amazon engineer who would have shipped this the night before his JPMorgan OA if he'd had it. Works on HackerRank, CodeSignal, CoderPad, and Karat.

Longest Palindromic Substring FAQ

What's the trick to Longest Palindromic Substring in O(n)?+

Manacher's algorithm. Transform the string with separators so every palindrome has a center character, then reuse mirrored radii inside the current rightmost palindrome. Total expansion work is linear because the right boundary only moves forward.

Will expand-around-center pass this Bloomberg OA?+

Probably not. It's O(n^2) worst case, and the stated constraint is O(n) with length up to 200000. A string of all the same letter would blow it up. Use it only to understand the idea, then write Manacher.

How do I handle the leftmost tie rule?+

Only update your best answer when a new radius is strictly greater than the stored one. Scanning left to right means the first palindrome of max length stays. Then compute the start as (center - radius) / 2 in the original string.

Is the dynamic programming hint misleading?+

Kind of. DP is the common approach for this problem title, but it needs O(n^2) time and space. The linear requirement here pushes you to Manacher, which is more a mirror-and-expand technique than a DP table.

How do I prepare for this in 48 hours?+

Write Manacher from scratch twice. Test on "cbbd", "babad", a single character, and a string of one repeated letter. Check the index mapping back to the original string, since that's where most bugs hide.

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