Merging Palindromes
Reported by candidates from Old Mission's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
A 26-slot frequency array is the whole solution to the Merging Palindromes question Old Mission candidates reported in March 2024. You're staring at two strings of up to 200000 characters, and the wording makes it sound like a nasty subset search. It isn't. It's counting plus one tricky rule about the middle character. If you've got the OA in a day or two, learn that rule and you're most of the way there. Most people lose time on the odd-count letters, not the pairs. StealthCoder sits invisibly on your screen during the live assessment as a safety net, so if the center-letter logic slips away mid-test, you still have a path to a correct answer.
The problem
From the letters of first, choose a multiset that can be rearranged into a palindrome. Do the same independently for second. Combine the two chosen multisets and rearrange them into one palindrome. Return the longest palindrome obtainable this way. If several have maximum length, return the lexicographically smallest. Function mergingPalindromes(first: String, second: String) → String Examples Example 1 first = "aabbc" second = "ddefefq" return = "abdefcfedba" Use every available pair from both strings. Among the remaining odd-count letters, c is the smallest possible center, producing the lexicographically smallest maximum-length palindrome. Example 2 first = "abc" second = "xyz" return = "a" No character forms a pair in either string. A one-character palindrome is optimal, and a is lexicographically smallest. Constraints 1 <= first.length, second.length <= 200000 Both strings contain lowercase English letters only.
Reported by candidates. Source: FastPrep
Pattern and pitfall
Count letters in each string. Every letter contributes floor(count/2) pairs, and those are always usable, so sum them across both strings. The trick is the leftover odd letters. Each string can keep at most one unpaired letter, since each chosen multiset must itself be palindromic. If some letter is odd in both strings, each string spends its single odd slot on it and the two singles merge into an extra pair. That's worth 2 characters, so it beats a lone center worth 1. Pick the smallest such letter. If no letter is odd in both, take the smallest odd letter from either string as the center. Build the half by sorting pair letters ascending, then mirror it. The common pitfall is greedily picking a center first and missing the merged pair. StealthCoder is the hedge if you blank on that case during the live OA.
If this hits your live OA and you blank, StealthCoder solves it in seconds, invisible to the proctor.
You can drill Merging Palindromes 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 StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass Old Mission's OA.
Old Mission 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.
Merging Palindromes FAQ
What's the trick in Merging Palindromes?+
Each string may keep only one unpaired letter, but two matching unpaired letters across strings combine into a pair. So check for a letter with odd count in both strings first. That adds 2 characters. Only if none exists do you fall back to a single center letter.
How do I get the lexicographically smallest answer?+
Build the left half by writing pair letters in sorted order, then mirror it for the right half. For the extra merged pair, use the smallest letter odd in both strings. For the center, use the smallest odd-count letter available. Smaller letters earlier always wins.
What's the time complexity I should aim for?+
Linear. With strings up to 200000 characters, one pass over each string fills two 26-element count arrays. Building the output is linear in its length. Anything involving subsets, permutations, or sorting the full string repeatedly is unnecessary and risks timing out.
What edge cases should I test?+
No pairs at all, like abc and xyz, where the answer is the single smallest letter. A letter odd in both strings. Letters with count 3 or 5, which give pairs plus a leftover. Cases where no odd letter exists, so no center is added.
How do I prepare for this in 48 hours?+
Practice frequency-array problems about palindromes from character counts, then write this solution once from memory. Focus on the three steps: sum the pairs, check common odd letters, otherwise pick a center. Run both examples by hand. That's enough, since the problem has no deep algorithm.