-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathfind-interview-candidates.sql
162 lines (147 loc) · 5.24 KB
/
find-interview-candidates.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
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.
*/
# V0
select name, mail
from Users
where user_id in (
select gold_medal as user_id
from Contests
group by gold_medal
having count(gold_medal) >= 3
union all
(with contest_and_medal as (
select contest_id, gold_medal as user_id
from Contests
union all
select contest_id, silver_medal as user_id
from Contests
union all
select contest_id, bronze_medal as user_id
from Contests)
select distinct c1.user_id
from contest_and_medal c1,
contest_and_medal c2,
contest_and_medal c3
where c1.contest_id - c2.contest_id = 1
and c2.contest_id - c3.contest_id = 1
and c1.user_id = c2.user_id
and c1.user_id = c3.user_id))
# V1
# https://circlecoder.com/find-interview-candidates/
select name, mail
from Users
where user_id in (
select gold_medal as user_id
from Contests
group by gold_medal
having count(gold_medal) >= 3
union all
(with contest_and_medal as (
select contest_id, gold_medal as user_id
from Contests
union all
select contest_id, silver_medal as user_id
from Contests
union all
select contest_id, bronze_medal as user_id
from Contests)
select distinct c1.user_id
from contest_and_medal c1,
contest_and_medal c2,
contest_and_medal c3
where c1.contest_id - c2.contest_id = 1
and c2.contest_id - c3.contest_id = 1
and c1.user_id = c2.user_id
and c1.user_id = c3.user_id))
# V2
# Time: O(nlogn)
# Space: O(n)
WITH winners_cte AS
((SELECT gold_medal AS winner, contest_id
FROM contests)
UNION ALL
(SELECT silver_medal AS winner, contest_id
FROM contests)
UNION ALL
(SELECT bronze_medal AS winner, contest_id
FROM contests)),
consecutive_winners_cte AS
(SELECT winner, contest_id, row_number() OVER (PARTITION BY winner ORDER BY contest_id) AS row_num
FROM winners_cte),
candidates_cte AS
((SELECT winner AS user_id
FROM consecutive_winners_cte
GROUP BY winner, contest_id - row_num
HAVING count(1) >= 3
ORDER BY NULL)
UNION
(SELECT gold_medal AS user_id
FROM contests
GROUP BY gold_medal
HAVING count(1) >= 3
ORDER BY NULL))
SELECT u.name, u.mail
FROM users u
INNER JOIN candidates_cte c ON u.user_id = c.user_id;