Tennis Scoring Classes: Game, Set and a Deuce/Advantage Score Display

Quick Overview

Implement tennis scoring as classes: a Game that records points, reports the score and the winner and rejects invalid calls, a Set built from games with a rule for six games all, and a display of the called score using 0, 15, 30, 40, deuce and advantage. It tests state modeling, win-by-two rules, composition and error handling.

Tennis Scoring Classes: Game, Set and a Deuce/Advantage Score Display

Company: Reddit

Role: Software Engineer

Category: Software Engineering Fundamentals

Difficulty: medium

Interview Round: Technical Screen

Implement scoring for a tennis match between two players, `A` and `B`, in three steps: a single game, then a set made of games, then a readable display of a game's score. No input or output format was given, so agreeing on the API, how points are reported and what each method returns is part of the task. In the reported round there was not enough time left to finish the third part, so pace the first two. Standard tennis scoring applies: - A game is won by the first player to win at least 4 points with a lead of at least 2 points. A player's points are called `0`, `15`, `30` and `40`. When both players have 40, the score is deuce; from deuce, the player who wins the next point has the advantage, and then either wins the game with the following point or the score returns to deuce. This can repeat any number of times. - A set is won by the first player to win at least 6 games with a lead of at least 2 games. What happens at 6 games all is a rule to confirm (Part 2). ### Clarifying Questions - How are points reported: one call per point naming the player who won it, or a whole sequence such as `"AABBA"`? What does each method return? - What should happen when a point is recorded after the game is already won, or for a player other than `A` or `B`: raise an error, return an error value, or ignore the call? Must the score be unchanged after a rejected call? - What should the winner method return while the game is still in progress? ### Part 1 — Game Write a `Game` class that can record a point won by a given player, return the current score, and return the winner, with handling for invalid calls. ```hint Minimal state Decide what the smallest state is that determines everything about a game, and whether the called values 15, 30 and 40 need to be stored at all. ``` #### What This Part Should Cover - A minimal game state and a correct win-by-two rule, including any number of deuce and advantage cycles - A clear API for recording a point, reading the score and reading the winner, with defined behavior during and after the game - Error handling for an unknown player and for a point recorded after the game is over ### Part 2 — Set Write a set class with the same kind of methods: record a point, return the score (games won by each player and the score of the current game), and return the winner. ```hint Who owns the point rules Consider whether the set needs to know how points work at all, or only needs to react when its current game produces a winner. ``` #### Clarifying Questions for this Part - At 6 games all, is a tiebreak game played to decide the set, or does play continue until one player leads by 2 games? - After the set is won, what should the score method report for the current game? #### What This Part Should Cover - Composing games: detecting a finished game, counting it and starting the next one - The set-winning rule, including the agreed behavior at 6 games all - Error handling consistent with the game class ### Part 3 — Display the game score Make the game's score method return the score as it is called in tennis, using `0`, `15`, `30`, `40`, `deuce`, `adv-A` and `adv-B`. ```hint Order the checks Some displays depend on both players' points together rather than on each player's points separately. Decide which situations must be checked before the plain per-player mapping. ``` #### Clarifying Questions for this Part - How are two called scores joined, for example `30-15`, and whose score comes first? - What should be displayed once the game is won? #### What This Part Should Cover - A correct mapping from points to calls, including deuce and advantage after any number of deuces - The display kept as a function of the game state, separate from the scoring rules - Tests at the boundaries: 40-30, the first deuce, an advantage lost back to deuce, and the winning point ### What a Strong Answer Covers - Settles the API, input format and error behavior with the interviewer before coding, since none were given - A minimal state model with each rule expressed in one place - Correct win-by-two logic for games and for sets - A set that reuses the game rather than duplicating point logic - Defined, tested behavior for invalid calls, with no change to the score on a rejected call - Working, tested code for the first two parts early enough to reach the display ### Follow-up Questions - Extend the design to a match won by the first player to take 2 of 3 sets (or 3 of 5). What new class is needed, and what stays unchanged? - Add an undo of the last recorded point. What state must you keep, and how does undo work across a game or set boundary? - Support no-advantage scoring, where the point played at deuce decides the game. Where does that rule live in your design? - Show the whole scoreboard, games in the set plus the current game, in one string. How do you avoid duplicating the display logic?

Overview: Implement tennis scoring as classes: a Game that records points, reports the score and the winner and rejects invalid calls, a Set built from games with a rule for six games all, and a display of the called score using 0, 15, 30, 40, deuce and advantage. It tests state modeling, win-by-two rules, composition and error handling.

Reddit logo
Reddit
Oct 23, 2026
mediumSoftware EngineerTechnical ScreenSoftware Engineering Fundamentals
0
0

Implement scoring for a tennis match between two players, A and B, in three steps: a single game, then a set made of games, then a readable display of a game's score. No input or output format was given, so agreeing on the API, how points are reported and what each method returns is part of the task. In the reported round there was not enough time left to finish the third part, so pace the first two.

Standard tennis scoring applies:

  • A game is won by the first player to win at least 4 points with a lead of at least 2 points. A player's points are called 0 , 15 , 30 and 40 . When both players have 40, the score is deuce; from deuce, the player who wins the next point has the advantage, and then either wins the game with the following point or the score returns to deuce. This can repeat any number of times.
  • A set is won by the first player to win at least 6 games with a lead of at least 2 games. What happens at 6 games all is a rule to confirm (Part 2).

Clarifying Questions Guidance

  • How are points reported: one call per point naming the player who won it, or a whole sequence such as "AABBA" ? What does each method return?
  • What should happen when a point is recorded after the game is already won, or for a player other than A or B : raise an error, return an error value, or ignore the call? Must the score be unchanged after a rejected call?
  • What should the winner method return while the game is still in progress?

Part 1 — Game

Write a Game class that can record a point won by a given player, return the current score, and return the winner, with handling for invalid calls.

What This Part Should Cover Guidance

  • A minimal game state and a correct win-by-two rule, including any number of deuce and advantage cycles
  • A clear API for recording a point, reading the score and reading the winner, with defined behavior during and after the game
  • Error handling for an unknown player and for a point recorded after the game is over

Part 2 — Set

Write a set class with the same kind of methods: record a point, return the score (games won by each player and the score of the current game), and return the winner.

Clarifying Questions for this Part Guidance

  • At 6 games all, is a tiebreak game played to decide the set, or does play continue until one player leads by 2 games?
  • After the set is won, what should the score method report for the current game?

What This Part Should Cover Guidance

  • Composing games: detecting a finished game, counting it and starting the next one
  • The set-winning rule, including the agreed behavior at 6 games all
  • Error handling consistent with the game class

Part 3 — Display the game score

Make the game's score method return the score as it is called in tennis, using 0, 15, 30, 40, deuce, adv-A and adv-B.

Clarifying Questions for this Part Guidance

  • How are two called scores joined, for example 30-15 , and whose score comes first?
  • What should be displayed once the game is won?

What This Part Should Cover Guidance

  • A correct mapping from points to calls, including deuce and advantage after any number of deuces
  • The display kept as a function of the game state, separate from the scoring rules
  • Tests at the boundaries: 40-30, the first deuce, an advantage lost back to deuce, and the winning point

What a Strong Answer Covers Guidance

  • Settles the API, input format and error behavior with the interviewer before coding, since none were given
  • A minimal state model with each rule expressed in one place
  • Correct win-by-two logic for games and for sets
  • A set that reuses the game rather than duplicating point logic
  • Defined, tested behavior for invalid calls, with no change to the score on a rejected call
  • Working, tested code for the first two parts early enough to reach the display

Follow-up Questions Guidance

  • Extend the design to a match won by the first player to take 2 of 3 sets (or 3 of 5). What new class is needed, and what stays unchanged?
  • Add an undo of the last recorded point. What state must you keep, and how does undo work across a game or set boundary?
  • Support no-advantage scoring, where the point played at deuce decides the game. Where does that rule live in your design?
  • Show the whole scoreboard, games in the set plus the current game, in one string. How do you avoid duplicating the display logic?