Find Interview Candidates Problem
Description
LeetCode Problem 1811.
Table: Contests
+--------------+------+
| Column Name | Type |
+--------------+------+
| contest_id | int |
| gold_medal | int |
| silver_medal | int |
| bronze_medal | int |
+--------------+------+
contest_id is the primary key for this table.
This table contains the LeetCode contest ID and the user IDs of the gold, silver, and bronze medalists.
It is guaranteed that any consecutive contests have consecutive IDs and that no ID is skipped.
Table: Users
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| user_id | int |
| mail | varchar |
| name | varchar |
+-------------+---------+
user_id is the primary key for this table.
This table contains information about the users.
Write an SQL query to report the name and the mail of all interview candidates. A user is an interview candidate if at least one of these two conditions is true:
The user won any medal in three or more consecutive contests. The user won the gold medal in three or more different contests (not necessarily consecutive). Return the result table in any order.
The query result format is in the following example:
Contests table:
+------------+------------+--------------+--------------+
| contest_id | gold_medal | silver_medal | bronze_medal |
+------------+------------+--------------+--------------+
| 190 | 1 | 5 | 2 |
| 191 | 2 | 3 | 5 |
| 192 | 5 | 2 | 3 |
| 193 | 1 | 3 | 5 |
| 194 | 4 | 5 | 2 |
| 195 | 4 | 2 | 1 |
| 196 | 1 | 5 | 2 |
+------------+------------+--------------+--------------+
Users table:
+---------+--------------------+-------+
| user_id | mail | name |
+---------+--------------------+-------+
| 1 | [email protected] | Sarah |
| 2 | [email protected] | Bob |
| 3 | [email protected] | Alice |
| 4 | [email protected] | Hercy |
| 5 | [email protected] | Quarz |
+---------+--------------------+-------+
Result table:
+-------+--------------------+
| name | mail |
+-------+--------------------+
| Sarah | [email protected] |
| Bob | [email protected] |
| Alice | [email protected] |
| Quarz | [email protected] |
+-------+--------------------+
Sarah won 3 gold medals (190, 193, and 196), so we include her in the result table.
Bob won a medal in 3 consecutive contests (190, 191, and 192), so we include him in the result table.
- Note that he also won a medal in 3 other consecutive contests (194, 195, and 196).
Alice won a medal in 3 consecutive contests (191, 192, and 193), so we include her in the result table.
Quarz won a medal in 5 consecutive contests (190, 191, 192, 193, and 194), so we include them in the result table.
Solution
Approach: Unpivot Medals and LEAD
Collect the candidates from the two conditions separately, then look up their names.
- Unpivot the medals.
UNION ALLthe gold, silver and bronze columns into one list of(contest_id, user_id)rows: one row per medal won. - Three consecutive contests. A user wins at most one medal per contest, so sort each user's medals by
contest_idand look two medals ahead withLEAD(contest_id, 2). If that contest iscontest_id + 2, the user medalled in three consecutive contests, because contest ids have no gaps. A longer streak simply matches several times. - Three gold medals. Group
Contestsbygold_medaland keep users withCOUNT(*) >= 3. - Combine both lists with
UNION, which lists each user once even when they meet both conditions or match step 2 several times (Quarz's five-contest streak matches three times), then join toUsers.
The Pandas version melts the medal columns, uses groupby(...).shift(-2) in place of LEAD, counts gold medals with value_counts, and keeps the users in either set.
WITH medals AS (
SELECT contest_id, gold_medal AS user_id FROM Contests
UNION ALL
SELECT contest_id, silver_medal FROM Contests
UNION ALL
SELECT contest_id, bronze_medal FROM Contests
),
next_medals AS (
-- The contest of this user's medal two medals later.
SELECT user_id,
contest_id,
LEAD(contest_id, 2) OVER (PARTITION BY user_id ORDER BY contest_id) AS third_contest_id
FROM medals
),
candidates AS (
SELECT user_id
FROM next_medals
WHERE third_contest_id = contest_id + 2
UNION
SELECT gold_medal
FROM Contests
GROUP BY gold_medal
HAVING COUNT(*) >= 3
)
SELECT u.name,
u.mail
FROM Users AS u
JOIN candidates AS c
ON c.user_id = u.user_id;
Now write it from memory
Close the write-up and solve it yourself against the tests.