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
โ€ข 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 details

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.

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
User 1 MedalsContest 5: GoldContest 6: SilverContest 7: Bronzeโ†ณ Consecutive!Apply ROW_NUMBER()contest_id | ROW_NUMBER()5 | 16 | 27 | 3Calculate Differencecontest_id - ROW_NUMBER()5 - 1 = 46 - 2 = 47 - 3 = 4โ†ณ Same group!Why This WorksConsecutive contests: 5,6,7Row numbers: 1,2,3Differences: 4,4,4 (same = consecutive)Non-Consecutive ExampleContests: 5,7,9 (gaps)Differences: 4,5,6 (different groups)Each forms separate group of 1SQL ImplementationWITH consecutive_medals AS (SELECT user_id, contest_id,contest_id - ROW_NUMBER()OVER (PARTITION BY user_id ORDER BY contest_id)as sequence_groupFROM all_medals)SELECT user_id FROM consecutive_medalsGROUP BY user_id, sequence_groupHAVING COUNT(*) >= 3
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Memory for CTEs and intermediate window function calculations

n
2n
โšก 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
Asked in
Google 45 Meta 38 Amazon 32 Microsoft 28
38.2K Views
High Frequency
~25 min Avg. Time
1.5K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen