Simulate a High-Card Deck Game for Two Players, Then N Players
Company: Affirm
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: hard
Interview Round: Technical Screen
Model and simulate a simple card game. Define your own classes and methods. The interviewer supplies test cases to check your code, so the design should let a test fix the order of the deck and read off the result.
A standard 52-card deck is shuffled and dealt so that each player holds a face-down pile. The game is played in rounds. In each round, every player turns over the top card of their pile, and the player whose card is higher wins the round: they take the other player's card and set it aside, together with their own card, in a pile of won cards. When the piles are used up, the player with the most won cards is the winner, and the program outputs the winner.
Start with two players. The follow-up generalizes the game to N players.
### Constraints and Clarifications
- The deck has 52 distinct cards: 13 ranks in each of 4 suits.
- Won cards are set aside, not returned to the player's pile, so every card is played exactly once.
### Clarifying Questions
- How are ranks ordered (for example, is the ace high), and do suits matter?
- What happens when the cards turned over in a round have equal rank?
- If two or more players finish with the same number of won cards, what should the program output?
- Should the program report only the winner, or also each player's count and a log of the rounds?
### Part 1 — Two players
Implement the two-player game: build and shuffle the deck, deal it, play every round, and output the winner. Show how a test fixes the deck order and checks the result.
```hint Make the shuffle testable
The shuffle is the only random step. Think about how a test can fix the order of the deck so that the expected winner is known in advance.
```
```hint Decide what a card is
Settle how two cards compare before writing the round loop; the answer to the tie question depends on that choice.
```
#### What This Part Should Cover
- Classes with clear responsibilities (such as card, deck, player and game) and a correct card comparison
- A round loop that moves both cards to the round winner's won pile and stops when the piles are empty
- Deterministic tests, including a decided game and a tie
### Part 2 — N players
Generalize the game to N players. With N players, 52 cards may not divide evenly. Deal round-robin, so that card `i` goes to player `i mod N` and the cards left over after an even split go to some players as one extra card each. For example, with three players one player receives 18 cards and the other two receive 17. The game must keep working when piles have different sizes.
```hint Uneven piles
Work out which players receive the extra cards, and what a round looks like once some piles are empty.
```
#### Clarifying Questions for this Part
- In a round with more than two players, does the highest card take every card turned over in that round?
- When some players have run out of cards, do the others keep playing, and what happens to a card turned over with no opponent?
- What range of N must be supported?
#### What This Part Should Cover
- A design in which the number of players is data, not separate code paths
- Round-robin dealing for any N, and the resulting pile sizes
- The rule for rounds in which some players have no cards left, and its fairness
- Tests for an uneven deal and for the accounting of all 52 cards
### What a Strong Answer Covers
- Asks about the rules the description leaves open (ranking, ties within a round, ties at the end) instead of guessing silently
- An object model in which going from two players to N players is a small change
- A card comparison that matches the agreed ranking for every rank, including the ten and the face cards
- Reproducible tests through an injectable shuffle or a fixed deck
- Invariants checked by tests, such as every card ending up in exactly one won pile
### Follow-up Questions
- How would you change the game so that won cards return to the bottom of the winner's pile and play continues until one player holds every card, and how would you make sure the simulation ends?
- How would you add a tie-breaker in which tied players each lay down extra cards and the next comparison decides who takes them all?
- How would you run many simulated games to estimate whether the player dealt an extra card has an advantage?
Overview: Model a shuffled 52-card game in which each round the higher top card wins both cards, then report the player with the most cards won. Starts with two players and generalizes to N players with uneven round-robin dealing, testing object design, rule clarification, and deterministic tests.
Read the full Affirm Software Engineer interview experience this question came from