KatInterview

Live Scoreboard Engine for One Tennis Game

Data Structure DesignModel answer
MediumModel answerData Structure Designsimulationstate-designclass-design

Last updated

Process a sequence of point winners for a single tennis game and print the correct score after each point, including deuce, advantage and the win-by-two rule.

You are writing the scoring engine behind a live tennis scoreboard. It covers one game, which is played one point at a time. Each point is won by player A or player B.

Input: an ordered sequence of symbols, each 'A' or 'B', giving the winner of each point.

Output: one line after every point, describing the game at that moment.

Scoring rules:

  • A player's score goes 0, 15, 30, 40 as they win their first, second and third point.
  • If both players have at least three points and are level, show Deuce. Never show 40-40.
  • If both players have at least three points and one is exactly one point ahead, show Advantage A or Advantage B.
  • A player wins the game by winning at least four points and leading by at least two. Show Game A or Game B.
  • Otherwise show A <score>-<score> B, for example A 30-15 B.

Example. The sequence A, B, B, B, B produces:

A 15-0 B
A 15-15 B
A 15-30 B
A 15-40 B
Game B

What to deliver:

  1. Explain what state you store and how each point updates it.
  2. Implement the engine in the language of your choice.
  3. State your policy for invalid input: a point after the game is already decided, or a symbol other than 'A' or 'B'.
  4. Give the outputs for the example above and for at least one sequence that passes through deuce.
  5. State the time and space complexity.

Hints

Hint 1

Don't make the printed labels (`15`, `Deuce`, `Adv`) your state. Ask what is the smallest piece of numeric information that fully determines the score.

Hint 2

Track how many points each player has won. The display is then a pure function of those two numbers. Test the conditions in order: game over, then deuce/advantage, then the plain 0/15/30/40 lookup.

Hint 3

Once both players have at least 3 points and the game isn't over, they can only be level or one apart, so long deuce battles need no extra state. Don't forget to reject points that arrive after the game has ended.