FastPrepTennis Game and Set Scoring

Tennis Game and Set Scoring

Reddit logoReddit● EasyFULLTIMEPHONE SCREEN

Problem statement

Build a scorekeeper for one tennis set between player A and player B. Process the strings in operations in order and return an array with one result string for each operation.

Scoring rules

  • A set is played as a series of games. Each point is won by exactly one player.
  • A player wins the current game as soon as they have at least 4 points and lead by at least 2 points. The next game then starts at 0 points for both players.
  • A player wins the set as soon as they have won at least 6 games and lead by at least 2 games. There is no tiebreak: at 6-6, play continues until one player leads by 2 games.

Operations

  • "POINT_A" or "POINT_B": that player wins a point. The result is "OK". If the set already has a winner, the point is not recorded and the result is "ERROR".
  • "GAME_SCORE": the score of the current game in the format below. If the set already has a winner, there is no current game and the result is "ERROR".
  • "SET_SCORE": the number of games each player has won, written as "gamesA-gamesB", for example "6-4".
  • "WINNER": "A" or "B" if that player has won the set, otherwise "NONE".

Game score format

  • If both players have at least 3 points and the same number of points, the score is "deuce".
  • If both players have at least 3 points and one player leads by exactly 1 point, the score is "adv-A" or "adv-B" for the leading player.
  • Otherwise, call each player's points as 0 points = "0", 1 = "15", 2 = "30", and 3 = "40". The score is player A's call, a hyphen, then player B's call, for example "30-15" or "0-40".

Function

scoreTennisSet(operations: String[]) → String[]

Examples

Example 1

operations = ["POINT_A","GAME_SCORE","POINT_B","POINT_B","GAME_SCORE","POINT_A","POINT_A","GAME_SCORE","POINT_A","SET_SCORE","GAME_SCORE","WINNER"]return = ["OK","15-0","OK","OK","15-30","OK","OK","40-30","OK","1-0","0-0","NONE"]

Player A wins the first point, so the game is 15-0. After B wins two points it is 15-30, and after A wins two more it is 40-30. The next point gives A 4 points against 2, a lead of 2, so A wins the game. The set score is 1-0, a new game starts at 0-0, and nobody has won the set yet.

Example 2

operations = ["POINT_A","POINT_A","POINT_A","POINT_B","POINT_B","POINT_B","GAME_SCORE","POINT_B","GAME_SCORE","POINT_A","GAME_SCORE","POINT_A","GAME_SCORE","POINT_A","SET_SCORE"]return = ["OK","OK","OK","OK","OK","OK","deuce","OK","adv-B","OK","deuce","OK","adv-A","OK","1-0"]

After three points each, both players have 3 points, so the score is deuce. B then leads by one point (adv-B). A ties it again (deuce) and then leads by one (adv-A). The next point gives A 6 points against 4, so A wins the game and the set score is 1-0.

Example 3

operations = ["POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","POINT_A","WINNER","SET_SCORE","POINT_B","GAME_SCORE"]return = ["OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","OK","A","6-0","ERROR","ERROR"]

Player A wins 24 points in a row, which is 6 games of 4 points each. With 6 games against 0, A wins the set. After that, the point for B is not recorded and asking for the game score is an error, because there is no current game.

Constraints

  • 1 <= operations.length <= 10^4
  • Each operations[i] is "POINT_A", "POINT_B", "GAME_SCORE", "SET_SCORE", or "WINNER".

More Reddit problems

See Reddit hiring insights
Saved
public String[] scoreTennisSet(String[] operations) {
    // Write your code here.
}
Checking account…