FastPrepLongest Palindrome from Two Words
Problems/Longest Palindrome from Two Words

Longest Palindrome from Two Words

Microsoft logoMicrosoft● HardFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A Microsoft senior MLE onsite report recalls choosing from each of two strings to form the longest new palindrome. The report does not say whether to use substrings or subsequences, whether both choices are required, or the output form. Here, we use nonempty subsequences and return the largest length. Our version is about 65% the same as the reported question.

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) → int

Examples

Example 1

word1 = "ab"word2 = "ba"return = 4

Select both words to make abba.

Example 2

word1 = "abc"word2 = "def"return = 0

No character in the first word matches one in the second, so a palindrome using both is impossible.

Example 3

word1 = "caa"word2 = "ac"return = 5

Select every character to make caaac.

Example 4

word1 = "ab"word2 = "cba"return = 5

Select 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)
All Microsoft interview questionsSee Microsoft hiring insights
Saved
class Solution {
    public int maxPalindromeFromTwoWords(String word1, String word2) {
        // Write your code here.
    }
}
Checking account…