← Salesforce Interview Insights

Salesforce·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Jun 2026

Summary

Salesforce SWE coding round with one fairly tricky string manipulation problem. The hint they gave about parity of character frequencies was actually useful but I didn't fully lean into it during the interview.

Questions Asked (1)

Q1

You have an array of n strings made up of lowercase letters. You can perform any number of operations where each operation swaps a single character between two different strings. What's the maximum number of strings that can be palindromes at the same time?

Algorithms & Data Structures
Author's notes

Took me a while to see what the hint was pointing at.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that the operation allows swapping any single character between any two strings, so the total multiset of characters across all strings is fixed. Then, determine the maximum number of strings that can be made palindromic by distributing characters appropriately, considering that a palindrome can have at most one character with an odd count (the middle character). Finally, derive a formula based on the total counts of each character and the number of strings.

Pro tip: After presenting your solution, mention that this problem is equivalent to maximizing the number of palindromes given a fixed character pool, and that the answer depends only on the parity of character counts and the number of strings. This shows you can abstract the problem to its core.

1. Understand the operation and constraints

Explain that swapping characters between strings preserves the total count of each character across all strings. The goal is to rearrange characters to form as many palindromes as possible.

2. Analyze palindrome requirements

A palindrome can have at most one character with an odd count (the middle character). All other characters must appear an even number of times.

3. Count odd frequencies

Compute the total count of each character and note how many characters have an odd total count. Let this number be O.

4. Derive the maximum number of palindromes

If O ≤ n, we can make all n strings palindromes by placing one odd-count character in each of O strings and pairing the rest. If O > n, we can make at most n - (O - n) = 2n - O strings palindromes. The correct formula is: if O ≤ n, answer is n; else answer is max(0, 2n - O). Each palindrome can absorb at most one odd count. So if O > n, we can have at most n palindromes, but we must leave some odd counts unpaired, which forces some strings to be non-palindromic. If O > n, we can still make n palindromes? Let's think: We have O odd counts. Each palindrome can take at most one odd count. So we can assign at most n odd counts to n palindromes. The remaining O - n odd counts must be paired up (two of the same character) to become even, but pairing two odd counts of the same character? No, odd counts are per character. If we have an odd count for a character, we can't just pair it with another odd count of a different character. To make a character's count even, we need to add or remove one occurrence of that character. But we can't remove characters; we can only redistribute. So if we have an odd count for a character, we can either put one of that character in a palindrome (as the middle) or we must pair it with another occurrence of the same character to make it even. But if the total count is odd, we cannot make it even without changing the total count. So the total count of each character is fixed. Therefore, if a character has an odd total count, it must contribute an odd number to some string. That string will have an odd count of that character, so it can be the middle character of a palindrome. But a palindrome can have only one odd count. So each palindrome can 'absorb' at most one character with odd total count. Therefore, if O > n, we can have at most n palindromes? But wait, if O > n, we have more odd-count characters than strings. Each palindrome can take at most one odd-count character. So we can assign at most n odd-count characters to the n strings. The remaining O - n odd-count characters must be placed in strings, but they will create additional odd counts in those strings, making them non-palindromic. So we cannot have all n strings be palindromes. In fact, each string can have at most one odd count to be a palindrome. So if we have O odd counts total, we need to distribute them among the strings. Each string can take at most one odd count if it is to be a palindrome. So the maximum number of palindromes is n if O ≤ n, and if O > n, we can have at most n - (O - n) = 2n - O palindromes? Let's test: Suppose n=2, O=3. Then 2n - O = 1. Can we have 1 palindrome? Yes, put two odd counts in one string (making it non-palindrome) and one odd count in the other (making it palindrome). So answer 1. If O=4, n=2, then 2n-O=0, but can we have 0 palindromes? Yes, put two odd counts in each string. So formula seems correct: max palindromes = n if O ≤ n, else 2n - O. But wait, is it always possible to achieve this? We need to ensure that we can pair up the even counts appropriately. Since we can swap any characters, we can always arrange the even counts to be paired within strings. So the formula holds. However, note that O is the number of characters with odd total count. Since the total number of characters is sum of counts, the parity of O is the same as the parity of the total number of characters? Not necessarily. But O can be at most 26. So if n is large, O ≤ n always, so answer is n. So the answer is min(n, 2n - O) but careful: if O ≤ n, answer n; else answer 2n - O. But 2n - O could be negative if O > 2n, but O ≤ 26, and n could be small. For example, n=1, O=26, then 2n-O = -24, but maximum palindromes is 0? Actually, with one string, can we make it a palindrome? Only if the string itself can be rearranged to a palindrome, which requires at most one odd count. But we have 26 odd counts total, so we cannot make it a palindrome. So answer 0. So formula should be max(0, 2n - O)? But if O > n, we can have at most n palindromes, but we need to subtract the excess odd counts. Each excess odd count beyond n forces one string to have at least two odd counts, making it non-palindromic. So the number of non-palindromic strings is at least O - n. So palindromic strings ≤ n - (O - n) = 2n - O. So answer = max(0, 2n - O) when O > n. But if O ≤ n, answer = n. So overall answer = min(n, 2n - O) but careful: if O ≤ n, 2n - O ≥ n, so min(n, 2n - O) = n. If O > n, 2n - O < n, so min(n, 2n - O) = 2n - O. So answer = min(n, 2n - O) but if 2n - O < 0, then answer 0. So answer = max(0, min(n, 2n - O)). But since O ≤ 26, and n can be small, we need max(0, ...). So final formula: if O ≤ n, answer = n; else answer = max(0, 2n - O). But note that O is the number of characters with odd total count. So we can compute O by counting frequencies. Then answer = n if O ≤ n else max(0, 2n - O). However, is it always possible to achieve this? We need to ensure that we can distribute the characters to form that many palindromes. Since we can swap arbitrarily, we can always arrange the even counts to be paired within strings. The only constraint is the odd counts. So yes, it's achievable. So the algorithm is: count frequencies of each character, count how many have odd frequency (O). Then if O ≤ n, return n; else return max(0, 2n - O). But wait, is there any other constraint? For example, if n=1 and the string has length 1, O=1, then O ≤ n, answer 1, correct. If n=1 and string has length 2 with two different characters, O=2, O > n, answer max(0, 2*1-2)=0, correct. So it works. So the solution is O(n + 26) time. But the problem asks for maximum number of strings that can be palindromes at the same time. So we need to output that number. So the candidate should present this reasoning.

5. Validate with examples

Test the formula with small cases, such as n=2 with strings 'ab' and 'cd' (O=4, answer 0), or n=3 with strings 'a', 'b', 'c' (O=3, answer 3).

Key Points to Mention

  • The operation preserves the total multiset of characters across all strings.
  • A palindrome can have at most one character with an odd count.
  • The maximum number of palindromes depends on the number of characters with odd total counts (O) and the number of strings (n).
  • If O ≤ n, all n strings can be palindromes; otherwise, the maximum is max(0, 2n - O).

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