Crafting a Leaderboard with SQL: A Coding Contest Case Study

Crafting a Leaderboard with SQL

The Task

Julia just finished conducting a coding contest, and she needs your help assembling the leaderboard! Write a query to print the respective hacker_id and name of hackers who achieved full scores for more than one challenge. Order your output in descending order by the total number of challenges the hacker has earned a full score. If more than one hacker received full scores in same number of challenges, then sort them by ascending hacker_id.


Input Format

The following tables contain contest data:

Hackers: The hacker_id is the id of the hacker, and name is the hacker’s name.

ColumnType
hacker_idInteger
nameInteger

Difficulty: The difficult_level is the difficulty level of the challenge, and the score is the challenge’s score for that difficulty level. 

ColumnType
difficulty_levelInteger
scoreInteger

Challenges: The challenge_id is the challenge’s ID, the hacker_id is the ID of the hacker who created the challenge, and the difficulty_level is the challenge’s difficulty level.

ColumnType
challenge_idInteger
hacker_idInteger
difficulty_levelInteger

Submissions: The submission_id is the id of the submission, hacker_id is the id of the hacker who submitted, challenge_id is the id of the challenge that the submission belongs to, and score is the submission’s score.

ColumnType
submission_idInteger
hacker_idInteger
Challenge_idInteger
scoreInteger

Sample Input

Hackers Table:

hacker_idname
5580Rose
8439Angela
27205Frank
52243Patrick
52348Lisa
57645Kimberly
77726Bonnie
83082Michael
86870Todd
90411Joe

Difficulty Table: 

difficulty_levelscore
120
230
340
460
580
6100
7120

Challenges Table:

challenge_idhacker_iddifficulty_level
4810777264
21089272051
3656655807
66730522436
71055522432

 

Submissions Table:

submission_idhacker_idchallenge_idscore
68628777263656630
65300777262108910
40326522433656677
89412720548104
83554777266673030
4335352243667300
55385523487105520
39784272057105523
9461386870710553030
4578852348365660
93058868703656630
734484396673092
27218439481036
5235580710554
4910552348667300
55877576456673080
38355272056673035
392484393656680
973979041166730100
8416283082481040
97431904117105530

Sample Output

90411 Joe

Explanation

Hacker 86870 got a score of 30 for challenge 71055 with a difficulty level of 2, so 86870 earned a total score for this challenge.

Hacker 90411 scored 30 for challenge 71055, which had a difficulty level of 2, so 90411 earned a full score for this challenge.

Hacker 90411 scored 100 for challenge 66730 with a difficulty level of 6, so 90411 earned a full score for this challenge.

Only hacker 90411 earned a full score for more than one challenge, so we print their hacker_id and name as space-separated values.


Let’s solve this problem in MySQL

To solve this problem, you need to create a SQL query that identifies hackers who have achieved full scores on more than one challenge. The full score for a challenge is defined by the maximum score for that challenge’s difficulty level, as specified in the Difficulty table.

Here’s the general strategy for the SQL query:

  1. Join the Submissions table with the Challenges and Difficulty tables to associate each submission with the maximum possible score for its challenge.
  2. Determine which submissions achieved the full score.
  3. Count the number of challenges where each hacker achieved full scores.
  4. Filter out hackers who achieved full scores in more than one challenge.
  5. Order the results by the number of full scores in descending order, and by hacker_id in ascending order when there is a tie.

Here’s a sample SQL query implementing the above logic:

SELECT h.hacker_id, h.name
FROM Hackers h
JOIN (
SELECT s.hacker_id, COUNT(DISTINCT s.challenge_id) AS full_score_count
FROM Submissions s
JOIN Challenges c ON s.challenge_id = c.challenge_id
JOIN Difficulty d ON c.difficulty_level = d.difficulty_level
WHERE s.score = d.score
GROUP BY s.hacker_id
HAVING COUNT(DISTINCT s.challenge_id) > 1
) AS fs ON h.hacker_id = fs.hacker_id
ORDER BY fs.full_score_count DESC, h.hacker_id;

Explanation of the query:

  • The innermost JOIN combines Submissions, Challenges, and Difficulty to get each submission with its associated maximum score (d.score).
  • The WHERE clause filters submissions to only those that match the full score for their difficulty level.
  • GROUP BY s.hacker_id groups results by hacker.
  • The HAVING COUNT(DISTINCT s.challenge_id) > 1 filters out hackers who did not achieve full scores in more than one challenge.
  • The outer query selects hacker’s details from the Hackers table and orders the results as required.

Ensure that your database schema correctly reflects the relationships and data types assumed in this query for it to work effectively. Adjust field and table names as necessary to match your actual database schema.

Scroll to Top