Running Total for Different Genders - Problem

Running Total for Different Genders

You're organizing a competitive scoring system for a mixed-gender tournament! ๐Ÿ† Each day, players from both the female team ('F') and male team ('M') score points in various competitions.

Your task is to calculate the cumulative running total for each gender on each day. This means you need to track how the total score builds up over time for both teams separately.

๐Ÿ“Š Table Structure: Scores

Column NameType
player_namevarchar
gendervarchar
daydate
score_pointsint

Key Points:

  • The combination (gender, day) forms the primary key
  • Gender is either 'F' (female) or 'M' (male)
  • Multiple players can score on the same day, but we aggregate by gender per day
  • You need to return results ordered by gender and day in ascending order

Goal: Calculate the running total score for each gender across all days, showing how the cumulative score grows over time.

Input & Output

example_1.sql โ€” Basic Tournament Scores
$ Input: Scores table: +-------------+--------+------------+-------------+ | player_name | gender | day | score_points| +-------------+--------+------------+-------------+ | Aron | F | 2020-01-01 | 17 | | Alice | F | 2020-01-07 | 23 | | Bajrang | M | 2020-01-07 | 7 | | Khali | M | 2020-01-07 | 18 | | Slaman | M | 2020-01-11 | 2 | +-------------+--------+------------+-------------+
โ€บ Output: +--------+------------+-------+ | gender | day | total | +--------+------------+-------+ | F | 2020-01-01 | 17 | | F | 2020-01-07 | 40 | | M | 2020-01-07 | 25 | | M | 2020-01-11 | 27 | +--------+------------+-------+
๐Ÿ’ก Note: Female team: Day 1 has 17 points (total: 17). Day 7 adds 23 points (running total: 17+23=40). Male team: Day 7 has 7+18=25 points (total: 25). Day 11 adds 2 points (running total: 25+2=27).
example_2.sql โ€” Same Day Multiple Players
$ Input: Scores table: +-------------+--------+------------+-------------+ | player_name | gender | day | score_points| +-------------+--------+------------+-------------+ | John | M | 2023-01-01 | 10 | | Jane | F | 2023-01-01 | 15 | | Bob | M | 2023-01-01 | 20 | | Alice | F | 2023-01-02 | 25 | +-------------+--------+------------+-------------+
โ€บ Output: +--------+------------+-------+ | gender | day | total | +--------+------------+-------+ | F | 2023-01-01 | 15 | | F | 2023-01-02 | 40 | | M | 2023-01-01 | 30 | +--------+------------+-------+
๐Ÿ’ก Note: On 2023-01-01: Female team scores 15, Male team scores 10+20=30. On 2023-01-02: Female running total becomes 15+25=40, Male team has no new scores so stays at 30.
example_3.sql โ€” Single Gender Tournament
$ Input: Scores table: +-------------+--------+------------+-------------+ | player_name | gender | day | score_points| +-------------+--------+------------+-------------+ | Sarah | F | 2023-01-01 | 30 | | Emma | F | 2023-01-02 | 20 | | Lisa | F | 2023-01-03 | 25 | +-------------+--------+------------+-------------+
โ€บ Output: +--------+------------+-------+ | gender | day | total | +--------+------------+-------+ | F | 2023-01-01 | 30 | | F | 2023-01-02 | 50 | | F | 2023-01-03 | 75 | +--------+------------+-------+
๐Ÿ’ก Note: Only female players participate. Running totals: Day 1: 30, Day 2: 30+20=50, Day 3: 50+25=75. This tests edge case where only one gender is present.

Constraints

  • 1 โ‰ค Number of rows in Scores table โ‰ค 1000
  • 1 โ‰ค score_points โ‰ค 1000
  • gender is either 'F' or 'M'
  • day is a valid date string in format 'YYYY-MM-DD'
  • The combination (gender, day) is unique in the table

Visualization

Tap to expand
๐Ÿ† Tournament Scoreboard: Running TotalsLive Tournament Dashboard๐Ÿ”ด Female Team Progress:Day 1Total: 17Day 7Total: 40+23๐Ÿ”ต Male Team Progress:Day 7Total: 25Day 11Total: 27+2๐ŸŽฏ Key Insights:โ€ข Separate running totals per genderโ€ข Chronological order within each genderโ€ข Daily scores are accumulatedโ€ข Window function partitions by genderโ€ข Single pass O(n log n) solution๐Ÿ“Š SQL Window Function ApproachSELECT gender, day,SUM(total_score) OVER (PARTITION BY gender ORDER BY day) as totalFROM (SELECT gender, day, SUM(score_points) as total_scoreFROM Scores GROUP BY gender, day) t
Understanding the Visualization
1
Daily Scores Collection
Each day, players compete and their scores are recorded with gender and date
2
Gender Partitioning
Separate the scores into male (M) and female (F) partitions
3
Chronological Processing
Process each gender's scores in date order to maintain running totals
4
Cumulative Display
Display running totals showing how each team's score grows over time
Key Takeaway
๐ŸŽฏ Key Insight: SQL window functions with PARTITION BY gender and ORDER BY day allow efficient calculation of running totals in a single pass, making this an optimal O(n log n) solution.
Asked in
Meta 35 Amazon 28 Microsoft 22 Google 18
23.4K Views
Medium Frequency
~15 min Avg. Time
856 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen