KatInterview

Replay Tennis Game Commands and Report Scores

Data Structure DesignAuto-graded · 14 test cases
EasyAuto-gradedData Structure Designsimulationstate-machinedesign

Last updated

Simulate a single tennis game from a command log, tracking points, deuce, advantage and the winner, and return each score query's text.

You are given a log of commands that drive a single game of tennis between two players, "Player A" and "Player B". Replay the log and produce one output entry per command.

Commands

Command i is commands[i], and its arguments are params[i] (a list of strings).

  • "TennisMatch": start a fresh game with both players at zero points and no winner. params[i] is empty. Output None.
  • "point_won_by": the player named in params[i][0] wins a point. Output None.
  • "score": report the current score. params[i] is empty. Output the score text described below.

Any other command name must raise a ValueError.

Scoring rules

Track how many points each player has won in the current game.

  • The first four points for a player are called Love, 15, 30, 40 (0, 1, 2, 3 points).
  • A player wins the game by having at least 4 points and leading the opponent by at least 2 points.
  • Once a player has won, the game is over. Any later point_won_by commands are ignored, with no change to the score and no error, until a new "TennisMatch" command resets the game.

Score text

For a "score" command, output exactly one of the following, checked in this order:

  1. If the game has been won: "Game " followed by the winner's name, for example "Game Player A".
  2. Else, if both players have at least 3 points:
    • equal points: "Deuce"
    • otherwise: "Advantage " followed by the name of the player who is ahead, for example "Advantage Player B".
  3. Otherwise: the call for Player A, a hyphen, then the call for Player B, for example "30-Love", "15-15", "Love-Love".

Note that 4–3 is Advantage, not a win, and a tie below 40 (such as 15–15 or 30–30) is shown as numbers, not Deuce. Long deuce battles can run on indefinitely (for example 10–10 is still Deuce).

Task

Return a list with one entry per command, in order: None for "TennisMatch" and "point_won_by", and the score string for "score".

Constraints

  • len(commands) == len(params).
  • Player names in point_won_by are always exactly "Player A" or "Player B".
  • Only a single game is modelled, with no sets or matches.
  • A game may be reset by additional "TennisMatch" commands in the same log.

Examples

Example 1

Input: replay_tennis_game(['TennisMatch', 'point_won_by', 'point_won_by', 'score'], [[], ['Player A'], ['Player A'], []])

Output: [None, None, None, '30-Love']

Example 2

Input: replay_tennis_game(['TennisMatch', 'point_won_by', 'point_won_by', 'point_won_by', 'point_won_by', 'point_won_by', 'point_won_by', 'score', 'point_won_by', 'score'], [[], ['Player A'], ['Player A'], ['Player A'], ['Player B'], ['Player B'], ['Player B'], [], ['Player B'], []])

Output: [None, None, None, None, None, None, None, 'Deuce', None, 'Advantage Player B']

Hints

Hint 1

Store only two integers, the points won by each player, and compute the displayed text from them whenever a score is requested.

Hint 2

Check conditions in a fixed order when formatting: game won first, then the both-at-least-3 zone (Deuce or Advantage), then the plain Love/15/30/40 lookup.

Hint 3

Winning needs at least 4 points and a lead of 2, so 4-3 is only Advantage. Remember to ignore points after the game is decided and to reset on a new TennisMatch command.