Problem Breakdown
Card Game
Data Structure Design
Published ·
medium
Try This Problem Yourself
Practice in the AI-enabled editor with real-time feedback
This problem hands you a solitaire-style card game and asks you to write the bot that plays it. Cards sit face-up in a grid, and a single move clears any three of them whose values add up to a fixed target. Clearing cards frees the board for more, and the game ends once no legal triple is left. Your solver's job is to clear as many cards as it can before it gets stuck.
The default game uses a 36-card deck (values 1 through 9, four suits each) dealt into a 3x4 grid. Each turn you pick 3 positions whose card values sum to 15, the engine removes them and refills from the deck, and play continues until no valid triple remains.
3x4 grid of 12 cards, with a triple whose values sum to 15 highlighted in teal
Values can repeat across positions, so two different 3s from different suits can appear together. The correctness tests all run on this small deck.
The optimization test scales things up by constructing a game with 100 cards (values 1–25), target sum 39, on a 10x10 grid where all 100 cards start on the board at once. There's no random refill, so the game is fully deterministic. Each turn removes exactly three cards, so the ceiling is 100 / 3 = 33 with one card stranded. Since 100 = 33 × 3 + 1, at most 33 full turns are possible. The test asks for an average of at least 32 over 30 seeds. This is where the difficulty ramp kicks in.
Your job is the Solver class. The engine hands you three methods for interacting with the game, letting you get the current grid as a map from position to card, play a move given three positions, and check whether any valid triple exists. On top of those, you fill in three Solver methods of your own covering triple-finding, a play loop, and an optimized play loop.
Solution 1, naive enumeration
The most natural approach is to check every combination of three grid positions, return the first triple whose values sum to the target, and loop until nothing is left on the board.
Complexity
Solution 2, 3-sum enumeration
Complexity
Solution 3, histogram lookahead
Why backtracking still doesn't work (even though it could)
Complexity
Benchmarks
Takeaways
Purchase Premium to Keep Reading
Unlock this article and so much more with Hello Interview Premium
Reading Progress
On This Page