Coding questions · Aeonea
Implement a tennis score tracker
This question evaluates a candidate's ability to implement stateful scoring logic and choose appropriate data structures for tracking sequential events, specifically handling tennis scoring rules such as 0/15/30/40 progression, Deuce, Advantage, and the two-point lead requirement.
Design and implement a tennis game scoring function. Input: a stream of points won by players 'A' or 'B' (e.g., A, B, B, B, B). After each point, output the current score in tennis notation: 'A 15-0 B', 'A 15-15 B', 'A 15-30 B', 'A 15-40 B', and finally the game result. Handle 0/15/30/40 progression, Deuce, Advantage, and Game with the correct two-point lead rule. Specify your data structures and update logic, and provide the outputs for the given example sequence.
Simulate Tennis Game Score Stream
Given a string points consisting of characters 'A' and 'B' representing the winner of each point in a single tennis game, return a list of the score after each processed point using tennis notation. Use 'A X-Y B' for normal scores where X,Y in {0,15,30,40}. When both players have at least 3 points and are tied, output 'Deuce'. When both have at least 3 and one leads by one point, output 'Advantage A' or 'Advantage B'. When a player has at least 4 points and leads by at least 2, output 'Game A' or 'Game B' and stop processing further characters (ignore any remaining input).
Constraints
- 1 <= len(points) <= 100000
- points[i] is either 'A' or 'B'
- The sequence contains at least the points up to and including the first 'Game' result; any extra characters after 'Game' must be ignored
- Output after each processed point using exactly one of: 'A X-Y B', 'Deuce', 'Advantage A', 'Advantage B', 'Game A', 'Game B'
Examples
Example 1
Input: points = 'ABBBB'
Output: ['A 15-0 B', 'A 15-15 B', 'A 15-30 B', 'A 15-40 B', 'Game B']
Explanation: B wins four of the first five points. Scores progress 15-0, 15-15, 15-30, 15-40, then B leads by 3 with at least 4 total points: 'Game B'.
Example 2
Input: points = 'ABABABABBB'
Output: ['A 15-0 B', 'A 15-15 B', 'A 30-15 B', 'A 30-30 B', 'A 40-30 B', 'Deuce', 'Advantage A', 'Deuce', 'Advantage B', 'Game B']
Explanation: The game reaches deuce at 40-40, then alternates advantage until B wins by two.
Hints
- Track raw point counts for A and B; map counts 0,1,2,3 to 0,15,30,40.
- If both have at least 3 points: tie -> 'Deuce'; one-point lead -> 'Advantage'; two-point lead -> 'Game'.
- Stop appending results immediately after emitting a 'Game' result and ignore remaining characters.
Approach
Maintain integer counters for points won by A and B. After each point, first check if a player has at least 4 points and leads by 2 or more; if so, append 'Game X' and stop. Otherwise, when both players have at least 3 points, output 'Deuce' if tied or 'Advantage X' if one leads by a single point. In all other cases, map raw points 0,1,2,3 to 0,15,30,40 and output 'A X-Y B'.
- Time complexity: O(n)
- Space complexity: O(n) for output, O(1) auxiliary