← Questions

Two-player card game simulation

0
AmazonCoding

A coding interview question asked at Amazon. It tests object-oriented design, simulation and queue.

You are building the engine for a simple two-player card game. Players are named "A" and "B". The deck is a list of distinct integers, and a higher integer beats a lower one.

Implement a CardGame class:

  • CardGame(deck, seed=None, shuffle=True): shuffle the deck (use seed so a game can be replayed), then deal it one card at a time, alternating players, starting with A. If the deck has an odd number of cards, the last card is not dealt.
  • play_round(): each player plays the top card of their hand. The player with the higher card puts both cards in their scoring pile. Return what was played and who won the round.
  • play() -> str: play rounds until the hands are empty. Return "A", "B", or "tie".
  • scores() -> dict: each player's score, one point per card in their scoring pile.

Model the game with sensible types (at least a player type). Keep the code easy to extend. The interviewer will add rules once the base game works.

Constraints

  • 0 <= len(deck) <= 10^5; card values are distinct integers.
  • play_round() after the game has ended is an error.

Example 1 (no shuffle)

deck = [5, 2, 1, 8, 7, 3]
A's hand: [5, 1, 7]   B's hand: [2, 8, 3]
Round 1: 5 vs 2 -> A    Round 2: 1 vs 8 -> B    Round 3: 7 vs 3 -> A
scores() == {"A": 4, "B": 2}; play() == "A"

Example 2 (no shuffle)

deck = [9, 1, 2, 8]
Round 1: 9 vs 1 -> A    Round 2: 2 vs 8 -> B
scores() == {"A": 2, "B": 2}; play() == "tie"

Likely follow-ups: allow repeated card values (tied rounds), support more than two players, and the War variant, where won cards go back into the winner's hand and the game may never end.

Clarifying questions

  • Is the 'top' of a hand the first card dealt to that player?
  • What should play_round() return: a small record of both cards and the winner?
  • Does play() continue from the current state if some rounds were already played?
  • With shuffle=True and no seed, is a non-reproducible shuffle fine?

What a strong answer covers

  • Models a Player (or enum) with a hand (a deque) and a scoring pile, and a RoundResult type for what each round returns.
  • Deals by alternating cards, dropping the last card when the deck is odd, and shuffles a copy with random.Random(seed) so games replay.
  • Each round is O(1); a full game is O(n) for n cards.
  • Raises a clear error on play_round() after the hands are empty, and handles an empty deck (play() returns "tie").
  • Keeps the comparison rule and the 'where won cards go' rule in one place so new rules slot in.
  • Tests with shuffle=False decks from the examples, an odd-sized deck, and a seed giving the same game twice.

Follow-ups

  • Allow repeated card values and decide what a tied round does.
  • Support more than two players.
  • Play the War variant, where won cards return to the winner's hand, and detect or cap a game that never ends.

Where it's asked

What it tests

object-oriented design · simulation · queue

Write and test your solution herePython in your browser, the question's tests level by level, every submission kept for you.Sign in to start coding