RedditTennis Match Scoring
Multi-part object-oriented design problem modeling a tennis match. Implement point / game / set / match scoring rules, then extend to best-of-three / best-of-five formats and run-time queries. The most common Reddit phone-screen prompt.
Problem
Design and implement a tennis-match scoring system. The prompt is structured as a sequence of parts, each unlocking more rules. Most loops cover parts 1–3; finishing parts 4–5 is the explicit staff-level signal.
- Part 1 — Game scoring. Model a single game. Two players accumulate points using the canonical tennis sequence (
0 / 15 / 30 / 40 / game). At 40-40 the game enters deuce; a player wins the game by leading by two points (advantage → game). The class exposes a method to record a point for a given player and a method to read the current displayed score. - Part 2 — Set scoring. Layer set tracking on top of games. A player wins a set by being the first to win 6 games and leading by ≥ 2 games. If the score reaches 6-6, play a tiebreak (first to 7, win by 2).
- Part 3 — Best-of-N matches. Generalize the match to best-of-three or best-of-five sets. Track set wins, expose the live match state, and signal match completion when a player wins ⌈N/2⌉ sets.
- Part 4 — Test harness. Drive the implementation from a list of point events and assert the final match state. Some interviewers ask for an in-class
runTestsmethod that consumes a string of point winners (e.g."AABBA…") and prints the score after each point. - Part 5 — Extension (often skipped due to time). Variants include serving rotation, history queries ("what was the score after the 17th point?"), or supporting both BO3 and BO5 polymorphically without re-implementing the game / set logic.
A typical interviewer pushes hard on clarifying questions up front — clarification on tiebreak rules, deuce semantics, and whether scoring methods return formatted strings ("15-30") or raw counts ((1, 2)) is part of the signal. Most candidates spend 15–20 minutes on clarification + OOD before writing any code.
Examples
Numeric layer (raw counts, deuce-collapse in action — see the two-part variant below):
p1, p2, p1, p1 -> get_score() == (3, 1); get_result() is None
p2, p2 -> (3, 3) # deuce
p1 -> (4, 3) # advantage p1
p2 -> (4, 4) collapses -> (3, 3) # back to deuceRender layer:
point alice -> score() == "15-love"
... reaching 3-3 -> score() == "deuce"
point alice -> score() == "advantage alice"
point bob -> score() == "deuce"
point bob, bob -> result() == "winner bob"Variants
Alternate canonical variant — two-part numeric Game + rendering Match
A commonly-seen shorter shape stops at a single game and splits the work into (1) a numeric point-counting API and (2) a human-readable renderer, replacing an explicit advantage counter with a deuce-collapse normalization. Assume exactly two players.
Part 1 — numeric Game API. Store raw point counts; expose:
class Game:
def __init__(self, player1: str, player2: str): ...
def add_score(self, player: str) -> None: ...
# Increment the given player's count; raise if the game already has a result.
def get_score(self) -> tuple[int, int]: ...
# Current raw counts as (player1_count, player2_count).
def get_result(self) -> str | None: ...
# Winner's name once max(count) >= 4 AND lead >= 2; else None.The trick that keeps this branch-free: normalize a tie back to 3-3 the instant it forms above deuce. On each add_score, if both counts are ≥ 4 and equal, reset both to 3. So 4-4 → 3-3, and tied states such as 5-5 never persist as observable scores — deuce naturally reappears after an advantage is surrendered, and get_result only ever fires from a 4-3/3-4 → +1 step. Both get_score and get_result stay O(1) with no advantage bookkeeping; space is O(1).
Part 2 — rendering Match. Wrap a Game and translate raw counts to tennis terms:
class Match:
def __init__(self, player1: str, player2: str): ...
def point_won_by(self, player: str) -> None: ... # delegates to Game.add_score
def score(self) -> str: ...
def result(self) -> str | None: ... # "winner <player>" or NoneRendering rules for score():
- Per-side label map
0→"love" / 1→"15" / 2→"30" / 3→"40"; below3-3render as"{a}-{b}"(e.g."15-love","30-30"). - Both counts ≥ 3 and equal →
"deuce"(raw3-3is tennis 40-40). - Both ≥ 3 and unequal (raw
4-3/3-4) →"advantage <leader>". - A decided game →
"winner <player>".
Game → Set → pretty-print ordering
Another phone-screen ordering keeps the score numeric until the very end:
- Part 1 —
Gamewith get-score, record-score, and get-winner methods, plus explicit error-case handling. No input / output format is given; agreeing on it with the interviewer is part of the task. - Part 2 —
Setwith the same family of methods (get score, record a game, get winner). - Part 3 — pretty-print the game score: render
Game.get_scoreas0 / 15 / 30 / 40 / deuce / adv-A / adv-B. In a 60-minute screen this part is easy to leave unfinished, so keep Parts 1–2 tight.
Pitfalls
- Common failure mode: candidate finishes parts 1–2 cleanly but runs out of time on part 3 because the set / game state was tangled into a single class. The interviewer will not pause the clock to refactor.
- Multiple interviewers explicitly ask candidates to write their own tests rather than relying on hidden cases. Plan for the last 5–10 minutes to be test-writing, not coding.
- Typical time budget for the multi-part version: ~20 minutes for Part 1 plus its tests, ~15 minutes for Part 2 plus tests. Get Part 1 fully working and tested before moving on.
- Firing
get_resulton lead-≥-2 alone without also requiringmax(count) >= 4, which wrongly ends the game at an early2-0. - Skipping the deuce-collapse, so
advantage → deuce → advantagecycling instead accumulates unbounded counts and the render logic misclassifies later tied states. - Flip-flopping between returning formatted strings and raw tuples across methods; keep the numeric layer (
get_score→ tuple) and the render layer (score()→ string) cleanly separated. - Error cases are graded: recording a point for an unknown player, or after the game / set already has a winner, should be rejected explicitly rather than silently ignored.
Follow-ups
Preparation
- Drill the deuce → advantage → game state machine on paper until you can write it in under 3 minutes without bugs. This is the densest correctness check in part 1.
- Practice the layered OOD: write
Gamefirst end-to-end (with tests), then wrap it inSet, then inMatch. Resist the urge to design all three classes upfront — interviewers reward incremental, testable layering. - Time-box yourself: 15 minutes clarification + design, 10 minutes per part. If part 1 takes longer than 15 minutes of coding, the loop is in trouble.
- Pre-write a
runTests("AABB…")driver in your scratch space — having it ready saves the last 10 minutes for the multi-part variants. - Decide ahead of time whether to expose the score as a formatted string or a structured object; interviewers accept either but punish flip-flopping mid-round.
- Write the raw-count →
0 / 15 / 30 / 40 / deuce / adv-A / adv-Brenderer as a standalone 10-line function so it can be bolted on in the last minutes.
