Design Tennis Scoring with Pluggable Rules
RedditBackend Engineer · Technical ScreenLast updated
Design a testable tennis game scoring component with point recording, readable scores, terminal-state handling, and stable player identity. Keep standard Deuce and Advantage rules separate from pluggable alternatives such as no-ad scoring, and discuss audit history and serialization.
Design Tennis Scoring with Pluggable Rules
Prompt
Design a small scoring component for one tennis game between two players. It must record which player won each point, report a human-readable score, and identify when the game has ended and who won.
Under standard scoring, the first points are displayed as Love, 15, 30, and 40. Once both players have at least three points, equal scores are Deuce, a one-point lead is Advantage, and a two-point lead wins the game. Extend the design so another scoring policy, such as no-ad scoring at Deuce, can be selected without putting rule-specific conditionals throughout the game object.
Candidate hint: Keep the facts that happened separate from the policy that interprets those facts.
Constraints & Assumptions
- Only one game is in scope; set and match scoring are out of scope.
- Calls after a game is complete must have defined behavior.
- Player identity must not depend on display names being unique.
- The score policy may control display and terminal-state rules.
Clarifying Questions to Ask
- Should the component preserve a point-by-point audit trail or only current counts?
- Should invalid calls raise errors or return explicit result objects?
- Can the scoring policy change after a game begins?
Follow-up questions
- How would you add undo for a mistakenly recorded point?
- What changes if scores must be reconstructed from an event log?
- How would you serialize a game without serializing executable strategy objects?
- Which cases belong in a table-driven test suite?
Solution walkthrough is for members
Get the full walkthrough with reference code and complexity analysis, plus Kitty Coach at 300 messages a day. Hints, the code runner and community answers stay free.