Katkat

Design a Tennis Scoring System

RedditMachine Learning Engineer · Technical Screen

Last updated

This problem tests your ability to model state transitions, encapsulate logic using object-oriented principles, and implement conditional rule-based scoring in a competitive game.

Problem Statement

Design a class TennisMatch to track the score of a standard tennis game between two players, "Player A" and "Player B". Your class should expose a method to register a point win and a method to retrieve the current score in the standard tennis format.

Scoring Rules

  1. Points are scored in the sequence: 0 (Love), 15, 30, 40.
  2. If both players reach 40, the score is "Deuce".
  3. After Deuce, the next point gives the player "Advantage".
  4. If a player with Advantage wins the next point, they win the game.
  5. If the player with Advantage loses the point, the score returns to "Deuce".
  6. A player wins the game if they score 4 points (and the opponent has 2 or fewer) or if they win from Advantage.

Requirements

Implement the following interface:

class TennisMatch:
    def __init__(self):
        # Initialize match state
        pass

    def point_won_by(self, player_name: str) -> None:
        # Update the score based on who won the point
        pass

    def score(self) -> str:
        # Return current score as a string (e.g., "15-30", "Deuce", "Advantage Player A")
        pass

Examples

  • Input: match.point_won_by("Player A"), match.point_won_by("Player A") -> Output: "30-Love"
  • Input: After 40-40, match.point_won_by("Player B") -> Output: "Advantage Player B"

Constraints

  • You do not need to track sets or matches, only a single game.
  • Assume player_name is always either "Player A" or "Player B".
  • The game ends immediately once a player wins.

Hints

Hint 1

Represent the score internally using integers (0, 1, 2, 3) rather than strings to simplify state transition logic.

Hint 2

Handle the 'Deuce' and 'Advantage' scenarios as special states that occur only when both players have at least 3 points.

Hint 3

Consider using a dictionary or a mapping array to translate integer point values (0-3) into their respective string representations (Love, 15, 30, 40).

Follow-up questions

  • How would you extend your class to keep track of the number of games won by each player in a set?
  • What changes would be required to support a 'Tie-break' scenario if the score reaches 6-6 in games?
  • How would you modify the design if the match scoring rules changed to a different format, such as 'No-Ad' scoring?