Solve Two Backtracking Array Problems
Company: Meta
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Technical Screen
Quick Answer: This prompt evaluates algorithmic problem-solving skills focused on combinatorial search and backtracking, including handling duplicate values, index distinctness, and enforcing character-uniqueness constraints across concatenated subsets.
Part 1: Find Three Cards Summing to 15
Constraints
- `0 <= len(cards) <= 2000`
- `-10^9 <= cards[i] <= 10^9`
- Indices in the answer must be distinct
- Duplicate card values may appear
Examples
Input: ([2, 7, 4, 8, 6, 1],)
Expected Output: [0, 1, 4]
Explanation: Values at indices 0, 1, and 4 are 2, 7, and 6, which sum to 15.
Input: ([5, 5, 5, 5],)
Expected Output: [0, 1, 2]
Explanation: There are multiple valid triples, so return the lexicographically smallest one.
Input: ([20, -5, 0, 5, 10],)
Expected Output: [0, 1, 2]
Explanation: 20 + (-5) + 0 = 15.
Input: ([1, 2, 3, 4],)
Expected Output: []
Explanation: No three values sum to 15.
Input: ([15],)
Expected Output: []
Explanation: Edge case: fewer than three cards.
Hints
- Try fixing the first two indices and asking what third value is needed to reach 15.
- To make the answer deterministic, scan `i` and `j` from left to right and choose the earliest valid `k > j`.
Part 2: Maximize Unique Characters from a Word List
Constraints
- `0 <= len(words) <= 16`
- `1 <= len(words[i]) <= 26`
- `words[i]` contains only lowercase English letters
Examples
Input: (['ab', 'cd', 'aef', 'gh'],)
Expected Output: 7
Explanation: One best choice is 'cd' + 'aef' + 'gh', which has length 7 and all unique characters.
Input: ([],)
Expected Output: 0
Explanation: Edge case: choosing nothing gives length 0.
Input: (['aa', 'bc', 'def'],)
Expected Output: 5
Explanation: The word 'aa' is invalid because it repeats a character internally, so the best is 'bc' + 'def'.
Input: (['ab', 'bc', 'cd'],)
Expected Output: 4
Explanation: You cannot use 'ab' and 'bc' together, but 'ab' + 'cd' is valid and has length 4.
Input: (['abcdefghijklmnopqrstuvwxyz', 'ab', 'cd'],)
Expected Output: 26
Explanation: The single 26-letter word already uses every lowercase letter exactly once.
Hints
- First discard any word that already contains a repeated character inside itself.
- Represent each valid word as a 26-bit mask so overlap checks become very fast.