KatInterview

Pick the Title Winner from Mixed-Orientation Game Records

SQL & DataAuto-graded · 14 test cases
MediumAuto-gradedSQL & Datadata-manipulationjoinsfiltering

Last updated

Given team and game record lists, find the 2023 championship winner when the winner may be on either side, with a deterministic highest-score tie-break.

Determine the Season Champion

You have two collections of records, modeled after relational tables and passed in as lists of dictionaries.

teams: each dictionary has

  • team_id (int): unique identifier
  • team_name (str): display name

games: each dictionary has

  • game_id (int): unique identifier
  • date (str): game date
  • season_year (int): the season the game belongs to
  • team_id (int): the team recorded as the primary side
  • opponent_team_id (int): the team recorded as the other side
  • team_score (int): points scored by team_id
  • opponent_team_score (int): points scored by opponent_team_id
  • game_type (str): for example "CHAMPIONSHIP" or "REGULAR"

Task

Return the team_name of the team that won the 2023 championship.

Rules

  1. Only consider games where season_year == 2023 and game_type == "CHAMPIONSHIP" (exact, case-sensitive match). Use season_year, not the year in date.
  2. The winner of a game is the side with the higher score. The champion may be stored in either team_id or opponent_team_id, so do not assume which column holds it.
  3. If both scores in a single game are equal, treat the team_id side as the winner.
  4. Normally there is exactly one qualifying game. If data problems produce several qualifying games, pick the game whose winning score is highest. If several share that highest winning score, use the one that appears first in games.
  5. Do not hard-code any team ids or names. Use only the two inputs.
  6. The winner's id is guaranteed to exist in teams, and at least one qualifying game is guaranteed to exist.

Constraints

  • 1 <= len(teams), 1 <= len(games)
  • All scores are non-negative integers.
  • team_id values in teams are unique.
  • At least one game satisfies the filter in rule 1.

Examples

Example 1

Input: get_2023_champion_name([{'team_id': 1, 'team_name': 'UConn Huskies'}, {'team_id': 2, 'team_name': 'San Diego State Aztecs'}, {'team_id': 3, 'team_name': 'Alabama Crimson Tide'}, {'team_id': 4, 'team_name': 'Purdue Boilermakers'}], [{'game_id': 1001, 'date': '2023-04-03', 'season_year': 2023, 'team_id': 1, 'opponent_team_id': 2, 'team_score': 76, 'opponent_team_score': 59, 'game_type': 'CHAMPIONSHIP'}, {'game_id': 1101, 'date': '2024-01-15', 'season_year': 2024, 'team_id': 3, 'opponent_team_id': 4, 'team_score': 88, 'opponent_team_score': 79, 'game_type': 'REGULAR'}])

Output: 'UConn Huskies'

Hints

Hint 1

Each game row only describes one perspective, so first decide, per qualifying row, which id belongs to the higher score.

Hint 2

Build a dictionary from team_id to team_name so that looking up the winner's name is constant time.

Hint 3

Keep the best (winning score, name) pair seen so far, and only replace it when a strictly higher winning score appears, so that earlier rows win ties.