KatInterview

Build a Flexible Tennis Game Score Tracker

Data Structure DesignModel answer
MediumModel answerData Structure Designobject-oriented-designstrategy-patternstate-management

Last updated

Create a scoring system for a single tennis game that records points, displays readable scores, detects game completion, and supports multiple rule variants.

Problem

You need to implement a scoring tracker for a single tennis game between two players. The system must accomplish four things:

  1. Record points — track which player wins each point as the game progresses
  2. Display current score — present the score in standard tennis terminology (Love, 15, 30, 40, Deuce, Advantage)
  3. Detect game end — recognize when the game finishes and identify the winner
  4. Support rule variations — allow different scoring rules (standard vs. no-ad) without scattering conditionals throughout your code

Standard Tennis Scoring Rules

  • The first four points are called Love (0), 15, 30, and 40
  • When both players reach 40 (three points each), the score is Deuce
  • From Deuce, winning a point gives you Advantage
  • From Advantage, if you win another point you win the game; if your opponent wins, it returns to Deuce
  • A player wins by reaching four points with at least a two-point margin

Requirements

  • Player identity must be stable regardless of name changes or duplicates
  • Behavior after game completion must be well-defined (no undefined states)
  • Rule variants should be encapsulated, not mixed into game logic
  • The design should be testable and maintainable

Things to Consider

  • Should you store the full point history or just current tallies?
  • How do you handle invalid operations (scoring after game ends, etc.)?
  • Can the rule set change mid-game, or is it fixed at creation?
  • What's your API surface for creating games, recording points, and querying state?

Hints

Hint 1

Separate the record of what happened from the interpretation of those events

Hint 2

Consider the Strategy pattern to encapsulate rule variations

Hint 3

Think about immutability vs. mutation for score state