Longest Palindrome

Algorithms
Easy
Amazon
123.7K views

Given a string $s$ which consists of lowercase or uppercase letters, find the length of the longest palindrome that can be built from those letters. Case sensitive.

Why Interviewers Ask This

Amazon asks this to evaluate your ability to optimize space complexity while handling edge cases like mixed-case characters. They specifically test if you can recognize that palindrome construction relies on character frequency parity rather than substring continuity, assessing your analytical thinking and efficiency mindset.

How to Answer This Question

1. Clarify constraints: Confirm case sensitivity and whether spaces or special characters exist. 2. Analyze the core logic: Explain that a palindrome needs pairs of identical characters, with at most one unique character allowed in the center. 3. Choose the data structure: Propose using a Hash Map or Frequency Array to count occurrences of each character efficiently. 4. Derive the algorithm: Iterate through counts; add even counts fully, and for odd counts, add (count - 1) to allow pairing, tracking if any odd count exists to add 1 later. 5. Validate edge cases: Discuss scenarios like empty strings or single-character inputs. 6. Optimize: Mention time complexity O(n) and space complexity O(1) due to fixed character set size.

Key Points to Cover

  • Recognizing that character order does not matter, only frequency counts
  • Correctly handling the single-center character rule for odd-length palindromes
  • Distinguishing between uppercase and lowercase as distinct entities
  • Achieving O(n) time complexity with minimal auxiliary space usage
  • Clearly articulating the mathematical logic behind summing even counts and subtracting one from odds

Sample Answer

To solve the longest palindrome problem from a string containing mixed-case letters, I first clarify that since it is case-sensitive, 'A' and 'a' are distinct. The key insight here is that we don't need contiguous substrings; we just need to rearrange characters. A valid palindrome consists of pairs of matching characters. If a character appears an even number of times, all instances can be used. If it appears an odd number of times, we can use all but one instance to form pairs, leaving one remainder. At most, one character with an odd count can sit in the exact center of the palindrome. My approach involves initializing a frequency map to count every character in the input string. As I iterate through the string, I populate this map. Next, I traverse the map values. For each count, if it is even, I add it directly to my result length. If it is odd, I add the count minus one, effectively discarding the center candidate temporarily. I also maintain a boolean flag indicating if I've encountered any odd count. After processing all characters, if the flag is true, I increment the total length by one to account for that central character. This ensures the solution runs in O(n) time with O(1) space relative to the alphabet size, which aligns well with Amazon's focus on efficiency and clean, optimal code.

Common Mistakes to Avoid

  • Attempting to find the longest palindromic substring instead of building from available characters
  • Forgetting to handle the single center character when all character counts are odd
  • Ignoring case sensitivity and treating 'A' and 'a' as the same character
  • Using nested loops to compare characters instead of utilizing a hash map for counting

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 145 Algorithms questionsBrowse all 73 Amazon questions