Merging Palindromes
Reported by candidates from Zscaler's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Zscaler reported this one in September 2024, and the statement is sneakier than it looks: pick a palindrome-capable multiset from each string separately, then merge both picks into a single palindrome. Example 2 returns just "a" from "abc" and "xyz", which tells you a lone center character is a legal answer. It's a counting problem with a greedy finish, not a DP. If you've got the OA invite and 200000-length strings in the constraints, brute force is dead on arrival. Count letters, build the half, pick the center. StealthCoder sits invisibly on your screen as a safety net if you blank mid-assessment, but the logic below is short enough to own.
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 all available pairs and the smallest usable center. The result has maximum length and is lexicographically smallest among maximum-length results. Example 2 first = "abc" second = "xyz" return = "a" No pair is available. Choose the smallest single character as the center. 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. For every letter, you get cnt1/2 pairs from the first string and cnt2/2 pairs from the second, using integer division. Here's the catch people miss: if a letter has an odd count in both strings, the two leftover singles can be chosen together, since each string still has only one odd letter, and they merge into an extra pair. Count that pair. After that, letters odd in exactly one string are center candidates. Only one center is allowed in the final palindrome, so take the smallest such letter. Build the half by writing each letter in alphabetical order, repeated by its pair count. Output half + center + reversed half. Sorted half plus smallest center gives the lexicographically smallest maximum-length result. Pitfalls: forgetting the both-odd merge, and building the string with repeated concatenation instead of a list join. If you freeze live, StealthCoder is the hedge, running unseen while you finish.
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 Zscaler's OA.
Zscaler 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 actual trick in Merging Palindromes?+
Treat it as letter counting. Each string contributes floor(count/2) pairs per letter. A letter that's odd in both strings yields one bonus pair when merged. Then pick the smallest letter that's odd in only one string as the center. Sort the half ascending and mirror it.
Why does a letter odd in both strings matter?+
Each string is allowed one odd-count letter on its own. If both choose the same letter as their single leftover, the combined count is even, so it forms a valid pair in the final palindrome. Skipping this gives a shorter answer and fails hidden tests.
How do I get the lexicographically smallest result?+
Among maximum-length answers, put the smallest letters at the outer edges. Build the half by looping a to z and repeating each letter by its pair count. Choose the smallest eligible center. Then append the reverse of the half. No sorting call is needed beyond the alphabet loop.
Will it run in time for 200000-character strings?+
Yes. Counting is O(n) with 26 buckets, and building the output is O(n) if you collect pieces in a list and join once. Avoid string concatenation in a loop. Anything quadratic or any search over subsets will time out at this size.
How should I prepare for this Zscaler OA in 48 hours?+
Write the solution once from scratch using two 26-slot count arrays. Test it on both examples, then on edge cases: no pairs at all, the same odd letter in both strings, and all identical letters. That covers nearly every failure mode for this problem.