Coding questions · Atlassian
Label game performance by margin
This question evaluates proficiency in data manipulation and transformation by mapping numeric score margins to categorical labels and implementing vectorized operations in pandas as well as idiomatic R data-frame workflows.
Given a games DataFrame (or R data.frame) with columns: team_id, opponent_team_id, team_score, opponent_team_score, write code to: (1) Define a function that maps margin = team_score - opponent_team_score to labels: 'Blowout Win' (margin >= 20), 'Close Win' (1–19), 'Tie' (0), 'Close Loss' (-1 to -19), 'Blowout Loss' (<= -20). (2) Apply it to create a new column performance_label for every row. Provide both: (a) a vectorized Python solution using pandas (no per-row Python loop), and (b) an idiomatic R solution using dplyr or base R. Validate with this tiny example: team_id,opponent_team_id,team_score,opponent_team_score -> (1,2,95,70) => Blowout Win; (1,3,82,85) => Close Loss; (2,1,93,93) => Tie.
Label Game Performance by Score Margin
You are given a table games that records basketball game results with each row representing one game from the perspective of a particular team. Using SQL, write a query that returns all existing columns and adds a derived column performance_label based on the score margin defined as (team_score - opponent_team_score), using the following rules:
- 'Blowout Win' if margin >= 20
- 'Close Win' if margin BETWEEN 1 AND 19
- 'Tie' if margin = 0
- 'Close Loss' if margin BETWEEN -19 AND -1
- 'Blowout Loss' if margin <= -20
Return one row per game with the new performance_label column computed according to these rules.
Table games
| column | type | constraints |
|---|---|---|
| game_id | INT | PRIMARY KEY, NOT NULL |
| team_id | INT | NOT NULL |
| opponent_team_id | INT | NOT NULL |
| team_score | INT | NOT NULL |
| opponent_team_score | INT | NOT NULL |
Sample data:
| game_id | opponent_team_id | opponent_team_score | team_id | team_score |
|---|---|---|---|---|
| 1 | 2 | 70 | 1 | 95 |
| 2 | 3 | 85 | 1 | 82 |
| 3 | 1 | 93 | 2 | 93 |
| 4 | 1 | 81 | 3 | 100 |
| 5 | 5 | 98 | 4 | 77 |
| 6 | 4 | 69 | 5 | 89 |
| 7 | 7 | 81 | 6 | 80 |
| 8 | 6 | 94 | 7 | 75 |
| 9 | 9 | 90 | 8 | 91 |
Expected output
Return one row per game with all original columns plus a derived performance_label column that classifies the game outcome based on the score margin (team_score - opponent_team_score) according to the specified rules.
| game_id | opponent_team_id | opponent_team_score | performance_label | team_id | team_score |
|---|---|---|---|---|---|
| 1 | 2 | 70 | Blowout Win | 1 | 95 |
| 2 | 3 | 85 | Close Loss | 1 | 82 |
| 3 | 1 | 93 | Tie | 2 | 93 |
| 4 | 1 | 81 | Close Win | 3 | 100 |
| 5 | 5 | 98 | Blowout Loss | 4 | 77 |
| 6 | 4 | 69 | Blowout Win | 5 | 89 |
| 7 | 7 | 81 | Close Loss | 6 | 80 |
| 8 | 6 | 94 | Close Loss | 7 | 75 |
| 9 | 9 | 90 | Close Win | 8 | 91 |
Hints
- Compute the score margin using team_score - opponent_team_score.
- Use a CASE expression to map the margin ranges to the required labels.