Maximum-Length Unique-Character Subset
Company: Anthropic
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Onsite
Quick Answer: This question evaluates a candidate's ability to combine bitmasking with backtracking or subset enumeration to maximize a constraint over combinations of strings. It tests practical application of state-space search and character-set tracking, a common category in coding interviews assessing algorithmic problem-solving under small input bounds.
Constraints
- 1 <= len(words) <= 16
- 1 <= len(words[i]) <= 26
- Each words[i] consists of lowercase English letters ('a'-'z')
- The empty selection is valid and yields length 0
Examples
Input: (["un", "iq", "ue"],)
Expected Output: 4
Explanation: "un"+"iq"="uniq" uses u,n,i,q — all distinct, length 4. "un"+"ue" would repeat 'u', so 4 is the maximum.
Input: (["cha", "r", "act", "ers"],)
Expected Output: 6
Explanation: "act"+"ers" uses a,c,t,e,r,s — all distinct, length 6. Adding "cha" or "r" would repeat a letter.
Input: (["aa", "bb"],)
Expected Output: 0
Explanation: Every string has an internal duplicate letter, so none can be selected; the empty concatenation (length 0) is the only valid choice.
Input: (["a"],)
Expected Output: 1
Explanation: A single one-character string is valid on its own, giving length 1.
Input: (["abcdefghijklmnopqrstuvwxyz"],)
Expected Output: 26
Explanation: One string that already uses all 26 distinct letters yields the maximum possible length of 26.
Input: (["abcdefghijklmnopqrstuvwxyz", "abcdefghijklmnopqrstuvwxyz"],)
Expected Output: 26
Explanation: Both strings use every letter, so they conflict with each other; only one can be chosen, giving 26.
Input: (["ab", "cd", "ef"],)
Expected Output: 6
Explanation: All three strings are pairwise disjoint, so all can be concatenated for total length 6.
Input: (["abc", "bcd", "cde"],)
Expected Output: 3
Explanation: Every pair of strings shares at least one letter, so no two can be combined; the best is any single string of length 3.
Hints
- Represent each string as a 26-bit bitmask of the letters it uses. A string with an internal duplicate letter can be detected while building its mask (a bit is set twice) — skip those.
- Two selections are compatible iff their bitmasks share no bits (mask_a & mask_b == 0). The length of a combined selection is the popcount of the combined mask.
- Since n <= 16, enumerate all reachable disjoint combinations: start from {0} and, for each valid word mask, OR it into every existing reachable mask that doesn't conflict. Track the largest popcount seen.