Design an extensible rock paper scissors game with player stats
Company: NVIDIA
Role: Software Engineer
Category: Software Engineering Fundamentals
Difficulty: easy
Interview Round: Other
Design a Rock Paper Scissors game system that supports player registration, configurable win rules, and match play with game state tracking.
The system should be extensible to support additional moves (for example Lizard and Spock) by allowing custom win rules to be added dynamically. The default rules are: `rock` beats `scissors`, `scissors` beats `paper`, and `paper` beats `rock`. A new `RockPaperScissors()` instance starts with these three rules.
Players register with unique names. When two registered players play a round, the system determines the outcome based on the configured rules and updates each player's win, loss, and draw counts accordingly.
Implement the following methods:
- `add_rule(winner_move: str, loser_move: str) -> None`: add a win rule in which `winner_move` beats `loser_move`. This is how new moves enter the game.
- `register_player(name: str) -> bool`: register a new player. Returns `True` if successful, `False` if the name is already taken.
- `play_round(player1: str, move1: str, player2: str, move2: str) -> str`: play a round between two registered players. Returns `"<name> wins"` with the winner's name (for example `"Alice wins"`), `"draw"`, or `"invalid"`.
- `get_stats(name: str) -> str`: get a player's stats as a `"W:L:D"` string (wins:losses:draws). Returns `"invalid"` if the player is not found.
- `get_winner(move1: str, move2: str) -> str`: given two moves, return the winning move, `"draw"` if they are the same, or `"invalid"` if no rule covers the matchup.
A move is known once it appears in at least one rule. `play_round` returns `"invalid"` and changes no stats when either player is not registered, the two player names are the same, either move is unknown, or no rule decides between two different moves. `get_winner` returns `"invalid"` whenever either move is unknown, even if the two moves are equal.
In the first example, the game uses only the default rules.
```text
operations = ["RockPaperScissors", "register_player", "register_player", "play_round", "play_round", "play_round", "get_stats", "get_stats"]
arguments =
[
[],
["Alice"],
["Bob"],
["Alice", "rock", "Bob", "scissors"],
["Alice", "paper", "Bob", "paper"],
["Alice", "scissors", "Bob", "rock"],
["Alice"],
["Bob"]
]
Output: [null, true, true, "Alice wins", "draw", "Bob wins", "1:1:1", "1:1:1"]
```
Alice's rock beats Bob's scissors, both players then play paper for a draw, and Bob's rock beats Alice's scissors. Each player ends with one win, one loss and one draw.
In the second example, four rules for lizard and spock are added on top of the defaults.
```text
operations = ["RockPaperScissors", "register_player", "register_player", "add_rule", "add_rule", "add_rule", "add_rule", "play_round", "play_round", "get_winner", "get_stats"]
arguments =
[
[],
["Sheldon"],
["Raj"],
["rock", "lizard"],
["lizard", "spock"],
["spock", "scissors"],
["lizard", "paper"],
["Sheldon", "spock", "Raj", "scissors"],
["Sheldon", "lizard", "Raj", "paper"],
["lizard", "spock"],
["Sheldon"]
]
Output: [null, true, true, null, null, null, null, "Sheldon wins", "Sheldon wins", "lizard", "2:0:0"]
```
Spock beats scissors and lizard beats paper, so Sheldon wins both rounds. Lizard beats spock by the second added rule, and Sheldon's stats are 2 wins, 0 losses and 0 draws.
```hint Shape the rules for the question you ask
Every round asks the rules the same thing about an ordered pair of moves. Store them so that one lookup per direction answers it, and settle every invalid case before any counter changes.
```
### Constraints & Assumptions
- `1 <= number of players <= 100`
- Player names are non-empty, unique strings.
- Moves are lowercase strings (for example `"rock"`, `"paper"`, `"scissors"`, `"lizard"`, `"spock"`).
- A move cannot beat itself.
- Players must be registered before playing a round.
- Both players in a round must choose moves that appear in at least one configured rule.
### Clarifying Questions to Ask
- What should `add_rule` do when the reverse rule already exists, for example adding "scissors beats rock" on top of the defaults?
- Do rules ever need to be removed or replaced, or are they only added?
- Is in-memory state enough, or do players and stats need to persist across restarts?
### What a Strong Answer Covers
- The structures for rules, known moves and player stats, and the cost of each lookup.
- How a new move enters the game through `add_rule` without edits to the outcome logic.
- Every path to `"invalid"` in `play_round` and `get_winner`, with stats left unchanged on those paths.
- Draw handling, the winner's name in the result string, and the `W:L:D` format.
- Time and space complexity of each method.
### Follow-up Questions
- How would you detect and handle a contradictory rule, such as both "rock beats scissors" and "scissors beats rock"?
- How would you check that a rule set is complete, so that every pair of distinct moves has exactly one winner?
- How would you add a best-of-N match or a leaderboard sorted by wins?
- What changes if many games run at the same time and share one player registry?
Overview: Design an extensible rock paper scissors game class with player registration, configurable win rules, round play and win, loss and draw stats. It tests choosing lookup structures, handling every invalid input path and keeping the outcome logic open to new moves.