Go Fish

Turn-based card game

Design the card game Go Fish: a deck, hands grouped by rank, an ask that either takes cards or fishes, books of four, and a turn rule that decides who plays next.

Overview

Go Fish is played with a normal 52-card deck. On your turn you name a rank you already hold and ask one opponent for it. If they have cards of that rank they must hand over all of them and you ask again. If they have none, they say 'go fish' and you draw one card from the stock. Four cards of one rank make a book; you lay it down at once. The game ends when all thirteen books are on the table, and the player with the most books wins.

Interviewers like this problem because the rules are small but every rule has a place to live. The deck knows how to shuffle and draw. A hand knows which ranks it holds. A player owns a hand and a pile of books. The game owns the players, the stock, whose turn it is, and the rules. A strong answer keeps those four jobs apart, so the game never reaches into a hand to count cards and a hand never knows whose turn it is.

The part candidates get wrong is the turn rule. A turn does not always pass to the next player: a catch, a lucky fish or a completed book lets you go again. Get that right and write it as data the caller can read, not as a printed string.

Scoping the problem

Ask these questions before you write code; each answer changes a class.

  • Which rule set? Does a lucky fish give another turn? Does a book? These are the branches in the turn logic. The usual answer is Bicycle rules, where a catch, a lucky fish and a book all keep the turn.
  • How many players, and how many cards each? This sets the deal: 2 to 5 players, with seven cards each for 2 or 3 players and five for 4 or 5.
  • What happens when a player has no cards? An empty hand cannot ask. The player draws one from the stock on their turn, then asks, and is skipped if the stock is empty too.
  • Human players only, or computer players too? This decides whether move choice is part of the design. Humans only; a computer player is a follow-up.
  • Should the game be replayable in tests? This decides how randomness enters the deck. Yes, so inject the random source.
  • One process, or a networked game? This decides whether locks and an event log are needed. One process, single thread.

Functional requirements

  • Deal seven cards each to 2 or 3 players and five each to 4 or 5, one card at a time from a shuffled deck
  • The current player names a rank they hold and one opponent; asking for a rank they do not hold, or asking themselves, is refused
  • If the opponent holds the rank they hand over every card of it and the asker goes again
  • Otherwise the asker draws one card from the stock; drawing the asked rank keeps the turn, any other card passes it
  • Four cards of one rank are laid down as a book at once; making a book keeps the turn
  • A player with no cards draws one from the stock at the start of their turn; with an empty stock they are skipped
  • The game ends when the stock and every hand are empty; the player or players with the most books win

Non-functional requirements

  • has, take_all and book detection are O(1) per rank because the hand is keyed by rank
  • The random source is injected so a game can be replayed exactly in tests
  • Rule violations raise ValueError; the game state does not change on a refused ask
  • ask() returns a Turn result object that any UI or test can read; nothing is printed

Interview questions

  • Why key the hand by rank instead of keeping a list of cards?
  • How would you add computer players?
  • When should books be detected: after every card, or at the end of a turn?
  • Can the game get stuck? How does it end?
  • How would you support rule variants without rewriting the game?
  • Two players are in different browsers. What changes on the server?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio