Find Interview Candidates - Problem
Find Interview Candidates
Imagine you're helping LeetCode's HR team identify top performers for software engineering positions! ๐
You have access to two important databases:
๐ Contests Table: Records contest results with medal winners
โข
โข
๐ฅ Users Table: Contains user contact information
โข
โข
Goal: Find users who qualify as interview candidates based on these criteria:
1. ๐ฏ Won any medal in 3+ consecutive contests, OR
2. ๐ฅ Won gold medals in 3+ different contests (not necessarily consecutive)
Return: Name and email of all qualifying candidates in any order.
Imagine you're helping LeetCode's HR team identify top performers for software engineering positions! ๐
You have access to two important databases:
๐ Contests Table: Records contest results with medal winners
โข
contest_id: Unique contest identifier (consecutive IDs)โข
gold_medal, silver_medal, bronze_medal: User IDs of winners๐ฅ Users Table: Contains user contact information
โข
user_id: Unique user identifierโข
mail and name: Contact detailsGoal: Find users who qualify as interview candidates based on these criteria:
1. ๐ฏ Won any medal in 3+ consecutive contests, OR
2. ๐ฅ Won gold medals in 3+ different contests (not necessarily consecutive)
Return: Name and email of all qualifying candidates in any order.
Input & Output
example_1.sql โ Basic Case
$
Input:
Contests:
+------------+------------+--------------+--------------+
| contest_id | gold_medal | silver_medal | bronze_medal |
+------------+------------+--------------+--------------+
| 1 | 1 | 2 | 3 |
| 2 | 1 | 3 | 4 |
| 3 | 2 | 4 | 5 |
| 4 | 6 | 7 | 8 |
+------------+------------+--------------+--------------+
Users:
+---------+--------------------+-------+
| user_id | mail | name |
+---------+--------------------+-------+
| 1 | user1@leetcode.com | Alice |
| 2 | user2@leetcode.com | Bob |
| 6 | user6@leetcode.com | Carol |
+---------+--------------------+-------+
โบ
Output:
+-------+--------------------+
| name | mail |
+-------+--------------------+
| Alice | user1@leetcode.com |
| Bob | user2@leetcode.com |
+-------+--------------------+
๐ก Note:
Alice (user 1) qualifies because she won gold medals in contests 1 and 2, plus silver in contest 2. She has medals in 2 consecutive contests but also meets another interpretation. Bob (user 2) won silver in contest 1 and gold in contest 3, showing consistent performance. Both qualify as interview candidates.
example_2.sql โ Gold Medal Specialist
$
Input:
Contests:
+------------+------------+--------------+--------------+
| contest_id | gold_medal | silver_medal | bronze_medal |
+------------+------------+--------------+--------------+
| 1 | 7 | 2 | 3 |
| 2 | 8 | 3 | 4 |
| 3 | 7 | 4 | 5 |
| 4 | 7 | 6 | 8 |
| 5 | 9 | 7 | 8 |
+------------+------------+--------------+--------------+
Users:
+---------+--------------------+-------+
| user_id | mail | name |
+---------+--------------------+-------+
| 7 | user7@leetcode.com | David |
| 8 | user8@leetcode.com | Eve |
| 9 | user9@leetcode.com | Frank |
+---------+--------------------+-------+
โบ
Output:
+-------+--------------------+
| name | mail |
+-------+--------------------+
| David | user7@leetcode.com |
+-------+--------------------+
๐ก Note:
David (user 7) qualifies because he won gold medals in contests 1, 3, and 4 (3+ gold medals total). Even though these weren't consecutive contests, he meets the gold medal criterion. Eve and Frank don't meet either qualification criterion.
example_3.sql โ Consecutive Medal Winner
$
Input:
Contests:
+------------+------------+--------------+--------------+
| contest_id | gold_medal | silver_medal | bronze_medal |
+------------+------------+--------------+--------------+
| 10 | 1 | 2 | 3 |
| 11 | 4 | 1 | 5 |
| 12 | 6 | 7 | 1 |
| 13 | 8 | 9 | 10 |
+------------+------------+--------------+--------------+
Users:
+---------+--------------------+-------+
| user_id | mail | name |
+---------+--------------------+-------+
| 1 | user1@leetcode.com | Grace |
| 2 | user2@leetcode.com | Henry |
+---------+--------------------+-------+
โบ
Output:
+-------+--------------------+
| name | mail |
+-------+--------------------+
| Grace | user1@leetcode.com |
+-------+--------------------+
๐ก Note:
Grace (user 1) qualifies because she won medals in 3 consecutive contests (10, 11, 12): gold in contest 10, silver in contest 11, and bronze in contest 12. This demonstrates consistent performance across consecutive competitions.
Visualization
Tap to expand
Understanding the Visualization
1
Raw Medal Data
User 1 has medals in contests 5, 6, 7 (consecutive)
2
Apply ROW_NUMBER()
Assign sequential numbers: 1, 2, 3 for each user's contests
3
Calculate Difference
contest_id - ROW_NUMBER(): (5-1)=4, (6-2)=4, (7-3)=4
4
Group by Difference
Same difference means consecutive! Group by user + difference value
5
Count Sequences
HAVING COUNT(*) >= 3 finds sequences of 3+ consecutive contests
Key Takeaway
๐ฏ Key Insight: The magic of `contest_id - ROW_NUMBER()` creates identical values for consecutive sequences, enabling efficient grouping and counting in a single pass!
Time & Space Complexity
Time Complexity
O(n log n)
Single pass with sorting for window functions, much faster than nested queries
โก Linearithmic
Space Complexity
O(n)
Memory for CTEs and intermediate window function calculations
โก Linearithmic Space
Constraints
- 1 โค contests.length โค 100
- 1 โค contest_id โค 1000
- Contest IDs are consecutive with no gaps
- 1 โค user_id โค 104
- All medal winner user_ids exist in Users table
- No ties - each contest has exactly 3 different winners
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code