Longest Palindrome from Two Words
Updated
Problem statement
Given two lowercase strings word1 and word2, choose one nonempty subsequence from each string. Keep the original order of characters within each chosen subsequence, then concatenate the choice from word1 before the choice from word2.
Find the maximum possible length of a concatenation that is a palindrome. Return 0 if no such palindrome can use at least one character from both words. You may skip any characters in either word, but you may not reorder selected characters.
Function
maxPalindromeFromTwoWords(word1: String, word2: String) → intExamples
Example 1
word1 = "ab"word2 = "ba"return = 4Select both words to make abba.
Example 2
word1 = "abc"word2 = "def"return = 0No character in the first word matches one in the second, so a palindrome using both is impossible.
Example 3
word1 = "caa"word2 = "ac"return = 5Select every character to make caaac.
Example 4
word1 = "ab"word2 = "cba"return = 5Select every character to make abcba.
Constraints
1 <= word1.length, word2.length <= 500.- Both strings contain only lowercase English letters.
- The result is a length, not the palindrome text; any ties have the same answer.
More Microsoft Onsite Interview questions
- Bad Request AlertsONSITE INTERVIEW · Seen Nov 2026
- Longest Substring Without Repeating CharactersONSITE INTERVIEW · Seen Nov 2026
- Valid ParenthesesONSITE INTERVIEW · Seen Nov 2026
- Count a User's Actions in an Event LogONSITE INTERVIEW · Seen Nov 2026
- Smallest Batch Size Within a Step LimitONSITE INTERVIEW · Seen Nov 2026
- Count Subarrays with Equal Even and Odd CountsONSITE INTERVIEW · Seen Nov 2026
- Minimum Time to Rot All OrangesONSITE INTERVIEW · Seen Nov 2026
- Coin ChangeONSITE INTERVIEW · Seen Oct 2026
- Daily USD Totals with Effective-Dated Exchange RatesONSITE INTERVIEW · Seen Oct 2026
- Number Words to Digits in Any LanguageONSITE INTERVIEW · Seen Oct 2026
Similar questions at other companies
- Minimum Insertion Steps to Make a String PalindromeInfosys · ONSITE INTERVIEWInfosys interview questions
- Wildcard MatchingTripjack · ONSITE INTERVIEWTripjack interview questions
- Concatenated WordsAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Interleaving StringsCARS24 · ONSITE INTERVIEW
- Minimum Stickers to Form a Target StringByteDance · ONSITE INTERVIEWByteDance interview questions
- Longest Valid ParenthesesMathWorks · ONSITE INTERVIEWMathWorks interview questions
- Word Break IIBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Longest Valid ParenthesesMaven Clinic · ONSITE INTERVIEWMaven Clinic interview questions
- Word Break IIGoogle · ONSITE INTERVIEWGoogle interview questions
- Maximum Bouquet ValueOracle · ONSITE INTERVIEWOracle interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)