helencousins.com

Mastering SQL with HackerRank: Challenges Problem Explained

Written on

Understanding the HackerRank Challenges Problem

If you're aiming to enhance your SQL expertise, consistent practice is key. Developing new skills or habits demands regular engagement. Once you grasp the fundamental SQL syntax, tackling coding challenges in a practical context becomes essential. HackerRank is a fantastic platform that hosts numerous such problems. In this tutorial, I will explain the method to solve the "Challenges" problem (classified as Medium Difficulty in the Basic Joins section) and will provide a solution that generates the required output.

Overview of the "Challenges" Problem on HackerRank

Now, let's dive into the specifics of the problem definition. Julia assigned her students to develop coding challenges. The objective is to write a query that displays the hacker_id, name, and the total number of challenges created by each student. The results should be sorted by the total number of challenges in descending order. In cases where multiple students have created the same number of challenges, the sorting should then be done by hacker_id. Furthermore, if several students created the same number of challenges and that number is less than the maximum created, those students should be excluded from the results.

HackerRank provides two sample inputs and outputs. Below is the first set of input and output:

-- Hackers table

hacker_id | name |
5077 | Rose |
21283 | Angela |
62743 | Frank |
88255 | Patrick |
96196 | Lisa |

-- Challenges table

challenge_id | hacker_id |
61654 | 5077 |
58302 | 21283 |
40587 | 88255 |
29477 | 5077 |
1220 | 21283 |
69514 | 21283 |
46561 | 62743 |
58077 | 62743 |
18483 | 88255 |
76766 | 21283 |
52382 | 5077 |
74467 | 21283 |
33625 | 96196 |
26053 | 88255 |
42665 | 62743 |
12859 | 62743 |
70094 | 21283 |
34599 | 88255 |
54680 | 88255 |
61881 | 5077 |

-- Sample Output

hacker_id | name | challenges_created |
21283 | Angela | 6 |
88255 | Patrick | 5 |
96196 | Lisa | 1 |

Here is the second example of input and output:

-- Hackers table

hacker_id | name |
12299 | Rose |
34856 | Angela |
79245 | Frank |
80491 | Patrick |
81041 | Lisa |

-- Challenges table

challenge_id | hacker_id |
63963 | 81041 |
63117 | 79345 |
28225 | 34856 |
21989 | 12299 |
4653 | 12299 |
70070 | 79345 |
36905 | 34856 |
61136 | 80491 |
17234 | 12299 |
80308 | 79345 |
40510 | 34856 |
79820 | 80491 |
22720 | 12299 |
21394 | 12299 |
36261 | 34856 |
15334 | 12299 |
71435 | 79345 |
23157 | 24856 |
54102 | 34856 |
69065 | 80491 |

-- Sample Output

hacker_id | name | challenges_created |
12299 | Rose | 6 |
34856 | Angela | 6 |
79345 | Frank | 4 |
80491 | Patrick | 3 |
81041 | Lisa | 1 |

Formulating the SQL Query

The task is to write a query that retrieves the hacker_id, name, and challenges_created for each student. The results must be sorted in descending order by challenges_created and then in ascending order by hacker_id. Additionally, students who do not have the maximum challenges_created should be excluded unless they are the only ones with that count.

To start, we can determine the number of challenges created by each student without any exclusions:

SELECT

h.hacker_id,

h.name,

COUNT(c.challenge_id) challenges_created

FROM

hackers h INNER JOIN challenges c ON h.hacker_id = c.hacker_id

GROUP BY

h.hacker_id,

h.name

ORDER BY

3 DESC,

1

This query will yield the following results for the provided sample inputs:

-- Result for Sample Output 0

hacker_id | name | challenges_created |
21283 | Angela | 6 |
88255 | Patrick | 5 |
5077 | Rose | 4 |
62743 | Frank | 4 |
96196 | Lisa | 1 |

-- Result for Sample Output 1

hacker_id | name | challenges_created |
12299 | Rose | 6 |
34856 | Angela | 6 |
79345 | Frank | 4 |
80491 | Patrick | 3 |
81041 | Lisa | 1 |

Next, we need to filter out students who do not have a unique number of challenges created unless that number corresponds to the maximum challenges created by a single student. For the first sample input, Rose and Frank will be excluded since both have created 4 challenges while the maximum is 6. For the second sample output, both Rose and Angela will be retained since they have the highest count.

To determine the number of students who created the same number of challenges, we will add another query that groups the results from the first query by challenges_created:

WITH all_students AS (

SELECT

h.hacker_id,

h.name,

COUNT(c.challenge_id) challenges_created

FROM

hackers h INNER JOIN challenges c ON h.hacker_id = c.hacker_id

GROUP BY

h.hacker_id,

h.name

ORDER BY

3 DESC,

1

)

SELECT

challenges_created,

COUNT(*) num_students_created_this_chall

FROM

all_students

GROUP BY

challenges_created

This query will produce the following records for each sample input:

-- Result for Sample Output 0

challenges_created | num_students_created_this_chall |
6 | 1 |
5 | 1 |
4 | 2 |
1 | 1 |

-- Result for Sample Output 1

challenges_created | num_students_created_this_chall |
6 | 2 |
4 | 1 |
3 | 1 |
1 | 1 |

Now that we have these intermediate results, we can combine them and filter to include only those students who have either a unique number of challenges or have created the maximum number of challenges. Here’s the complete solution to the Challenges problem on HackerRank based on the outlined interpretation:

WITH all_students AS (

SELECT

h.hacker_id,

h.name,

COUNT(c.challenge_id) challenges_created

FROM

hackers h INNER JOIN challenges c ON h.hacker_id = c.hacker_id

GROUP BY

h.hacker_id,

h.name

ORDER BY

3 DESC,

1

),

num_students_per_num_challenges AS (

SELECT

challenges_created,

COUNT(*) num_students_created_this_chall

FROM

all_students

GROUP BY

challenges_created

)

SELECT

a.hacker_id,

a.name,

a.challenges_created

FROM

all_students a INNER JOIN num_students_per_num_challenges sc

ON a.challenges_created = sc.challenges_created

WHERE

sc.num_students_created_this_chall = 1

OR a.challenges_created = (SELECT MAX(challenges_created) FROM all_students)

Final Thoughts

In this brief tutorial, we've explored how to approach the Challenges problem on HackerRank and demonstrated a straightforward solution that yields the desired results. I hope this article has been helpful and easy to understand! Keep practicing and attempting as many challenges as possible.

Engage with Our Community

Thank you for being part of our community! Remember to express your support for this story and follow the author. You can also access more content in the Level Up Coding publication. If you're interested, check out our free coding interview course and join the Level Up talent collective to discover amazing job opportunities!

Share the page:

Twitter Facebook Reddit LinkIn

-----------------------

Recent Post:

Shedding My Skin: A Journey of Self-Discovery and Growth

A reflection on personal growth, letting go of negativity, and embracing self-love through life's challenges.

Understanding How Massive Stars Impact Planet Formation

A study of Westerlund 2 reveals how massive stars hinder planet formation due to their intense energy and stellar winds.

# Selecting the Right Framework for Your Mobile Application

Discover the best frameworks for mobile app development and factors to consider when choosing one.

Developers, Stop Sacrificing Your Time — It's Not Worth the Burnout

Developers must prioritize their well-being over unpaid overtime to avoid burnout and maintain a healthy work-life balance.

Understanding the Aging Process: A Deep Dive into Our Biological Decline

Explore the biological mechanisms behind aging, including genetic factors and lifestyle changes, to better understand this natural process.

Understanding Self-Deception: The Intricacies of Human Reasoning

Explore the complex nature of self-deception and its impact on human reasoning and belief systems.

How Mother Nature Helped Us Overcome the Pandemic Challenge

A look at how Omicron may have served as a natural vaccine against COVID-19.

Kobe Bryant's Psychological Edge: Lessons for Personal Growth

Explore Kobe Bryant's psychological advantages and how to apply them to your own life for personal growth and achievement.