InterviewDB Question

Card Game - OOD Deck, Hand, and Turn-Based Game Engine

Question Details

Problem

Design a class hierarchy for a simple turn-based card game. A standard deck has 52 cards (4 suits, 13 ranks). Players are dealt a hand. On each turn, a player plays one card; the highest-rank card wins the trick.

Implement:

python
class Card:
    suit: str  # HEARTS, DIAMONDS, CLUBS, SPADES
    rank: int  # 2-14 (14 = Ace)

class Deck:
    def __init__(self): ...          # 52 cards
    def shuffle(self, seed: int): ...
    def deal(self, n: int) -> list[Card]: ...

class Hand:
    def play(self, card: Card): ...
    def has_card(self, card: Card) -> bool: ...

class Game:
    def __init__(self, num_players: int): ...
    def play_round(self) -> str:

**returns** winner player id

Follow-ups

  1. How do you enforce that deal raises an error when the deck runs out of cards?
  2. How would you add a Trump suit that beats all other suits regardless of rank?
  3. Describe how you would serialize mid-game state to JSON for a save/resume feature.
  4. How do you handle games with wildcards (jokers) that can substitute for any card?

Full Details

Problem

Design a class hierarchy for a simple turn-based card game. A standard deck has 52 cards (4 suits, 13 ranks). Players are dealt a hand. On each turn, a player plays one card; the highest-rank card wins the trick.

Implement:

python
class Card:
    suit: str  # HEARTS, DIAMONDS, CLUBS, SPADES
    rank: int  # 2-14 (14 = Ace)

class Deck:
    def __init__(self): ...          # 52 cards
    def shuffle(self, seed: int): ...
    def deal(self, n: int) -> list[Card]: ...

class Hand:
    def play(self, card: Card): ...
    def has_card(self, card: Card) -> bool: ...

class Game:
    def __init__(self, num_players: int): ...
    def play_round(self) -> str:

**returns** winner player id

Follow-ups

  1. How do you enforce that deal raises an error when the deck runs out of cards?
  2. How would you add a Trump suit that beats all other suits regardless of rank?
  3. Describe how you would serialize mid-game state to JSON for a save/resume feature.
  4. How do you handle games with wildcards (jokers) that can substitute for any card?

About This Question

This is a reported interview question from a affirm interview during the phone round.

It covers the following topics: Coding, Ood, Onsite, Phone .