Pick the Title Winner from Mixed-Orientation Game Records
SQL & DataAuto-graded · 14 test casesLast 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 identifierteam_name(str): display name
games: each dictionary has
game_id(int): unique identifierdate(str): game dateseason_year(int): the season the game belongs toteam_id(int): the team recorded as the primary sideopponent_team_id(int): the team recorded as the other sideteam_score(int): points scored byteam_idopponent_team_score(int): points scored byopponent_team_idgame_type(str): for example"CHAMPIONSHIP"or"REGULAR"
Task
Return the team_name of the team that won the 2023 championship.
Rules
- Only consider games where
season_year == 2023andgame_type == "CHAMPIONSHIP"(exact, case-sensitive match). Useseason_year, not the year indate. - The winner of a game is the side with the higher score. The champion may be stored in either
team_idoropponent_team_id, so do not assume which column holds it. - If both scores in a single game are equal, treat the
team_idside as the winner. - 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. - Do not hard-code any team ids or names. Use only the two inputs.
- 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_idvalues inteamsare 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.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
Each game record is written from one team's point of view, so the champion can appear in either the team_id column or the opponent_team_id column. The winner therefore has to be derived from the scores, not from column position.
- Build a dictionary mapping
team_idtoteam_name. This plays the role of a join. - Scan the games and skip any row that is not a 2023
"CHAMPIONSHIP"game. - For each remaining row, compare the two scores. If
team_score >= opponent_team_score, the winner isteam_idwithteam_score. Otherwise the winner isopponent_team_idwithopponent_team_score. Using>=makes equal-score rows resolve to theteam_idside. - Track the candidate with the highest winning score. Replace the current best only on a strictly greater score, so the earliest row wins ties.
- Return the stored name.
Why it is correct
- Choosing the winner by comparing scores gives the right answer regardless of which column stores the champion. Swapping the two sides of a row never changes the result.
- If the same game is stored twice, once from each perspective, both rows normalize to the same winner and score, so the answer stays the same.
- If several conflicting championship rows exist, the highest winning score is selected, as the rules require. The strict comparison fixes the order of equal candidates.
- No ids or names are hard-coded, so the output depends only on the data.
Complexity
- Time: O(T + G), where T is the number of teams and G is the number of games.
- Space: O(T) for the name dictionary.
Reference solution
from typing import Dict, List
def get_2023_champion_name(teams: List[Dict], games: List[Dict]) -> str:
names = {row["team_id"]: row["team_name"] for row in teams}
best_score = None
best_name = None
for row in games:
if row["season_year"] != 2023 or row["game_type"] != "CHAMPIONSHIP":
continue
if row["team_score"] >= row["opponent_team_score"]:
score = row["team_score"]
name = names[row["team_id"]]
else:
score = row["opponent_team_score"]
name = names[row["opponent_team_id"]]
if best_score is None or score > best_score:
best_score = score
best_name = name
return best_name