Design a Tennis Scoring System
Company: Reddit
Role: Machine Learning Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Technical Screen
Design an object-oriented tennis scoring system for two named players.
Part 1: Implement a `TennisGame` class for a single game.
Required API:
```python
class TennisGame:
def __init__(self, player1: str, player2: str):
pass
def add_score(self, player: str) -> None:
pass
def get_score(self) -> tuple[int, int]:
pass
def get_result(self) -> str:
pass
def get_human_score(self) -> str:
pass
```
Rules:
- There are exactly two named players.
- Raw point values map to tennis terms as follows: `0 -> Love`, `1 -> 15`, `2 -> 30`, `3 -> 40`.
- A player wins a game only if they have at least 4 points and lead by at least 2 points.
- `get_result()` should return the winner name, or `''` if the game is still in progress.
- Once a game is over, calling `add_score()` again should raise an error or otherwise be rejected.
- Invalid player names should be rejected.
- At `3-3`, the game is in Deuce.
- If a player wins a point from Deuce, that player has Advantage.
- If the player with Advantage wins the next point, they win the game.
- If the player without Advantage wins the next point, the internal score should reset to `3-3` instead of growing unbounded as `4-4`, `5-5`, and so on.
Part 2: Extend the design to a `TennisSet` class composed of multiple games.
Required API:
```python
class TennisSet:
def add_score(self, player: str) -> None:
pass
def get_set_score(self) -> tuple[int, int]:
pass
def get_set_winner(self) -> str:
pass
```
Support these set modes:
- Simple mode: first player to win 3 games wins the set.
- Standard mode: first player to win at least 6 games and lead by at least 2 games wins the set. If the set reaches `6-6`, start a tie-breaker. For the tie-breaker, the first player to reach at least 7 points and lead by at least 2 wins the set.
When a game ends but the set is not over, the current game score should reset to `0-0` for the next game.
Part 3: Track player sides between games.
- Initially, player 1 starts on side `near` and player 2 starts on side `far`.
- Players switch sides after every odd-numbered completed game: after game 1, game 3, game 5, and so on.
- Side switching is presentational metadata only; it must not affect scoring logic.
- Provide a way to retrieve the current side assignment.
Describe your design, edge cases, and key tests.
Quick Answer: This question evaluates object-oriented design, state management, input validation, and the ability to model domain-specific scoring rules for a two-player game.