Shortest Suffix to Form a Palindrome
Reported by candidates from Bloomberg's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Most people blow the first attempt at this Bloomberg OA question by solving the wrong problem. It was reported in April 2020, and it looks like the classic shortest palindrome task, but you append to the end, not the front. Flip that and your answer is wrong on every non-trivial case. Given text, return the shortest string to append so the whole thing is a palindrome, or empty if it already is one. The hinted pattern is two-pointers. If you freeze on the direction, StealthCoder is the safety net running invisibly during the live OA.
The problem
Return the shortest string that can be appended to text so the result is a palindrome. If text is already a palindrome, return the empty string. Function shortestPalindromeSuffix(text: String) → String Examples Example 1 text = "banana" return = "b" Appending b creates bananab. Constraints 0 <= text.length <= 2000. The text contains lowercase English letters.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick: find the longest suffix of text that is already a palindrome. Everything before that suffix has to be mirrored and appended. So the answer is the reverse of text[0:i], where i is the start of the smallest-index palindromic suffix. With n up to 2000, brute force works. Loop i from 0 to n, check if text[i:] is a palindrome with two pointers, and on the first hit return the reverse of text[:i]. That's O(n^2), fine here. The common pitfall is copying the prepend version, which looks for the longest palindromic prefix and gives the wrong string. Check banana: the suffix anana is a palindrome at i=1, so you reverse 'b' and get 'b'. Also handle the empty string and the already-palindrome case, both return empty. If you blank on the direction mid-OA, StealthCoder is the hedge that reads the prompt and hands you the logic.
StealthCoder is the hedge for the one pattern you didn't drill. It runs invisibly during the screen share.
You can drill Shortest Suffix to Form 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. If you're reading this with an OA window open, you're who this was built for.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Bloomberg's OA.
Bloomberg reuses patterns across OAs. If you're reading this with an OA window open, you're who this was built for. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Shortest Suffix to Form a Palindrome FAQ
What's the trick for the Bloomberg shortest suffix palindrome question?+
Find the longest palindromic suffix of text. Whatever sits before it gets reversed and appended. Scan i from 0 upward, check text[i:] with two pointers, and return reverse(text[:i]) on the first palindrome. That first hit is the longest suffix, so the appended string is shortest.
How is this different from the LeetCode shortest palindrome problem?+
LeetCode's version prepends characters to the front, so you look for the longest palindromic prefix. Here you append to the end, so you look for the longest palindromic suffix. Mixing them up gives wrong output, for example returning 'ananab' style prefixes instead of 'b' for banana.
Is O(n^2) fast enough with length up to 2000?+
Yes. At most 2000 starting positions, each with a linear palindrome check, is about 4 million character comparisons in the worst case. That's fine. You don't need KMP or hashing unless you want to show off, and the simple version has fewer bugs.
What edge cases should I test?+
Test the empty string, a single character, an already-palindromic string like 'racecar', and a string with no palindromic suffix longer than one character like 'abcd', which should return 'abc'. Also run banana and expect 'b'. These cover the loop bounds and the empty-return rule.
How do I prepare for this in 48 hours?+
Write the brute-force solution from memory twice. Practice a palindrome checker with two pointers, then the loop that scans suffix starts. Run three or four hand cases. Spend leftover time on the KMP prefix-function version only if you already feel solid, since the brute force passes.