Optimize a Fifteen-Sum Card Strategy
Company: Meta
Role: Machine Learning Engineer
Category: Coding & Algorithms
Difficulty: hard
Interview Round: Onsite
Quick Answer: This question evaluates algorithmic problem-solving, debugging, simulation-based testing, and strategy optimization using techniques such as search, memoization, and dynamic programming in a combinatorial card game context.
Part 1: Safe Triple Removal in a Fifteen-Sum Move
Constraints
- 0 <= len(table), len(deck) <= 50
- Card values are integers in the range 1 to 9
- A valid move must remove exactly 3 cards from the current table
Examples
Input: ([1, 5, 9, 2, 4, 6], [1, 5, 9], [3, 7])
Expected Output: ([2, 4, 6, 3, 7], [], 15)
Explanation: A valid triple is removed, and only two cards are drawn because the deck has two cards left.
Input: ([5, 5, 1, 4, 9], [5, 5, 5], [2, 3, 4])
Expected Output: ([5, 5, 1, 4, 9], [2, 3, 4], 0)
Explanation: The table has only two 5s, so the move is invalid and nothing changes.
Input: ([8, 4, 3, 7], [8, 4, 3], [])
Expected Output: ([7], [], 15)
Explanation: The move is valid, but the deck is empty so no replacement cards are drawn.
Input: ([1, 2, 3], [1, 2, 3], [4])
Expected Output: ([1, 2, 3], [4], 0)
Explanation: The requested cards sum to 6, not 15, so the move is invalid.
Hints
- Treat the table and the requested triple as multisets, especially when duplicate values appear.
- Validate the move before removing anything, then remove only as many copies as the triple actually needs.
Part 2: Baseline Strategy for Choosing a Fifteen-Sum Triple
Constraints
- 0 <= len(table) <= 50
- Card values are integers in the range 1 to 9
Examples
Input: [1, 5, 9, 2, 4]
Expected Output: (0, 1, 2)
Explanation: The first scanned triple already sums to 15.
Input: [5, 5, 5]
Expected Output: (0, 1, 2)
Explanation: Duplicates are allowed as long as they come from different positions.
Input: [1, 2, 3, 4]
Expected Output: None
Explanation: No triple on the table sums to 15.
Input: []
Expected Output: None
Explanation: Edge case: an empty table has no valid move.
Hints
- The table is small enough that checking all 3-card combinations is acceptable.
- Use three nested loops with `i < j < k` so the first match is deterministic.
Part 3: Count Perfect Games in a Baseline Strategy Experiment
Constraints
- 0 <= len(games) <= 200
- For each game, 0 <= len(table) + len(deck) <= 36
- Card values are integers in the range 1 to 9
Examples
Input: [([1, 5, 9], []), ([1, 2, 3], [])]
Expected Output: 1
Explanation: Only the first game clears all cards.
Input: [([1, 5, 9], [2, 4, 9]), ([1, 5, 9, 2], [])]
Expected Output: 1
Explanation: The first game clears in two moves; the second leaves one card behind.
Input: []
Expected Output: 0
Explanation: No games means no perfect games.
Input: [([], []), ([], [1, 2, 3]), ([5, 5, 5], [])]
Expected Output: 2
Explanation: The empty-empty game and the all-5s game are perfect; the empty-table game with a leftover deck is not.
Hints
- Write a helper that simulates one full game using the same first-valid-triple rule every turn.
- A game is perfect only if both the table and the deck are empty at the end.
Part 4: Maximize Fifteen-Sum Score with Search and Memoization
Constraints
- 0 <= len(table) + len(deck) <= 20
- Card values are integers in the range 1 to 9
- Each move must remove exactly 3 table cards summing to 15
Examples
Input: ([1, 5, 9], [])
Expected Output: 15
Explanation: One move is possible.
Input: ([1, 2, 3], [])
Expected Output: 0
Explanation: No valid triple exists.
Input: ([1, 5, 9], [2, 4, 9])
Expected Output: 30
Explanation: The first move draws another valid triple, giving two moves total.
Input: ([1, 5, 9, 2, 4], [5, 8, 8])
Expected Output: 30
Explanation: Choosing 1+5+9 leads to another move, while choosing 2+4+9 does not.
Input: ([], [])
Expected Output: 0
Explanation: Edge case: no cards means no score.
Hints
- Try every valid triple from the current table, and recurse on the resulting state.
- Memoize by game state. Since only card values matter for future sums, sorting the table can merge equivalent states.