Magicsheet logo

Construct K Palindrome Strings

Medium
12.5%
Updated 8/1/2025

Construct K Palindrome Strings

What is this problem about?

The Construct K Palindrome Strings interview question gives you a string s and an integer k. You need to determine if it's possible to use all the characters in s to construct exactly k non-empty palindrome strings.

Why is this asked in interviews?

This Construct K Palindrome Strings coding problem is a frequent guest at Uber and Amazon. It tests a candidate's ability to identify the core properties of palindromes without actually building them. It's a "Greedy" logic problem where the character counts are more important than the character order.

Algorithmic pattern used

This follows the Hash Table, Counting, String, Greedy interview pattern. The solution is based on two observations:

  1. You cannot have more palindromes than characters (k > s.length is impossible).
  2. Each palindrome can contain at most one character with an odd frequency. Therefore, the minimum number of palindromes you can form is equal to the number of characters that appear an odd number of times in the original string.

Example explanation

s = "annabelle", k = 2

  1. Counts: a:2, n:2, b:1, e:2, l:2.
  2. Odd frequencies: Only 'b' appears an odd number of times (1 odd).
  3. Minimum palindromes needed = 1 (to accommodate 'b').
  4. Since 1 <= k <= 9 (length of string), it is possible to form 2 palindromes. For example: "annana" and "belle".

Common mistakes candidates make

  • Trying to build the strings: Attempting to use backtracking or permutations to find the palindromes, which is O(N!) and completely unnecessary.
  • Ignoring the upper bound: Forgetting to check if k is greater than the total number of characters.
  • Miscounting odds: Errors in character frequency counting.

Interview preparation tip

Whenever a problem asks if you "can construct" something based on a string, look for parity (even/odd) rules. Palindromes, in particular, are almost always about the number of characters with odd frequencies.

Similar Questions