← IBM Interview Insights

IBM·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
May 2026

Summary

IBM software engineer interview with a string manipulation problem that looks deceptively simple but has some real edge cases once you start thinking about odd-length strings and leftover characters.

Questions Asked (1)

Q1

You're given an array of lowercase strings. You can swap individual characters between any two strings as many times as you want, but each string's length must stay the same. What's the maximum number of strings you can turn into palindromes?

Algorithms & Data Structures
Author's notes

My first instinct was to think about this per-string, which was wrong.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, recognize that swapping characters between strings means we can redistribute characters arbitrarily as long as each string's length is preserved. The key is to count the frequency of each character across all strings, then determine how many strings can be made into palindromes by assigning characters appropriately. A palindrome can have at most one character with an odd count, so we need to check if the total odd counts can be accommodated within the number of strings.

Pro tip: Clarify that the order of characters within each string can be rearranged arbitrarily after swaps, so only the multiset of characters matters. Also, mention that the maximum number of palindromes is limited by the number of odd-frequency characters: each palindrome can absorb at most one odd count, so if there are more odd counts than strings, some strings cannot be palindromes.

1. Understand the operation

Explain that swapping characters between strings allows us to redistribute characters freely while preserving each string's length. Thus, the problem reduces to partitioning the multiset of all characters into strings of given lengths such that each string can be rearranged into a palindrome.

2. Count character frequencies

Compute the total frequency of each character across all strings. Also, note the lengths of each string, as they constrain how many characters each palindrome can have.

3. Determine odd counts

Identify how many characters have an odd total frequency. Each palindrome can contain at most one character with an odd count (the middle character). Therefore, the number of palindromes is limited by the number of odd counts.

4. Check feasibility with lengths

For each string, if its length is even, it must have zero odd counts; if odd, exactly one odd count. Use the total odd counts to see how many strings can satisfy this. Also, ensure that the total number of characters is sufficient to fill all strings.

5. Compute maximum palindromes

The maximum number of palindromes is the minimum of the number of strings and the number of odd counts (if odd counts > 0), but also consider that even-length strings cannot take odd counts. A greedy assignment works: assign one odd count to as many odd-length strings as possible, then use remaining characters to form palindromes for even-length strings.

Key Points to Mention

  • Swapping characters allows arbitrary redistribution, so only character frequencies matter.
  • A palindrome can have at most one character with an odd frequency.
  • The number of odd-frequency characters limits the number of palindromes.
  • String lengths constrain whether a string can be a palindrome (even length requires all even counts, odd length requires exactly one odd count).
  • Greedy assignment: use odd counts for odd-length strings first, then fill even-length strings with pairs.
  • Edge cases: empty strings, all characters same, more odd counts than strings.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.