Find Interview Candidates - Problem

You are given two tables: Contests and Users.

The Contests table contains information about LeetCode contests and their medalists:

  • contest_id: Unique identifier for each contest
  • gold_medal: User ID of the gold medalist
  • silver_medal: User ID of the silver medalist
  • bronze_medal: User ID of the bronze medalist

The Users table contains user information:

  • user_id: Unique identifier for each user
  • mail: User's email address
  • name: User's name

Task: Find all interview candidates. A user qualifies as an interview candidate if:

  1. They won any medal in three or more consecutive contests, OR
  2. They won the gold medal in three or more different contests (not necessarily consecutive)

Return the name and mail of all interview candidates in any order.

Table Schema

Contests
Column Name Type Description
contest_id PK int Unique contest identifier
gold_medal int User ID of gold medalist
silver_medal int User ID of silver medalist
bronze_medal int User ID of bronze medalist
Primary Key: contest_id
Users
Column Name Type Description
user_id PK int Unique user identifier
mail varchar User's email address
name varchar User's name
Primary Key: user_id

Input & Output

Example 1 — Consecutive Medals Winner
Input Tables:
Contests
contest_id gold_medal silver_medal bronze_medal
190 1 2 3
191 1 4 5
192 1 6 7
193 8 9 10
Users
user_id mail name
1 alice@leetcode.com Alice
2 bob@leetcode.com Bob
8 charlie@leetcode.com Charlie
Output:
name mail
Alice alice@leetcode.com
💡 Note:

Alice (user_id=1) won gold medals in 3 consecutive contests (190, 191, 192), qualifying her as an interview candidate. She also meets the consecutive medal requirement by winning any medal in 3+ consecutive contests.

Example 2 — Multiple Gold Winner
Input Tables:
Contests
contest_id gold_medal silver_medal bronze_medal
190 1 2 3
192 1 4 5
195 1 6 7
200 8 9 10
Users
user_id mail name
1 alice@leetcode.com Alice
8 charlie@leetcode.com Charlie
Output:
name mail
Alice alice@leetcode.com
💡 Note:

Alice (user_id=1) won gold medals in 3 different contests (190, 192, 195), qualifying her as an interview candidate based on the gold medal criterion. The contests are not consecutive, but that's not required for gold medal qualification.

Example 3 — No Qualified Candidates
Input Tables:
Contests
contest_id gold_medal silver_medal bronze_medal
190 1 2 3
195 4 5 6
Users
user_id mail name
1 alice@leetcode.com Alice
2 bob@leetcode.com Bob
Output:
name mail
💡 Note:

No user qualifies as an interview candidate. User 1 won only 1 gold medal (need 3+), and no user won medals in 3+ consecutive contests (only 2 contests total).

Constraints

  • 1 ≤ contest_id ≤ 1000
  • 1 ≤ gold_medal, silver_medal, bronze_medal ≤ 10^6
  • 1 ≤ user_id ≤ 10^6
  • All contest_id values are unique
  • Consecutive contests have consecutive IDs with no gaps

Visualization

Tap to expand
Find Interview Candidates INPUT Contests Table contest_id | gold | silver | bronze 190 | 1 | 5 | 2 191 | 2 | 3 | 5 192 | 5 | 2 | 3 Users Table user_id | name | mail 1 | Daniel | d@mail.com 2 | Alice | a@mail.com Qualification Rules 1 Gold in 3+ contests OR Any medal 3+ consecutive ALGORITHM STEPS 1 Count Gold Medals GROUP BY user, COUNT gold wins 2 Check Consecutive Find 3+ medals in a row 3 Union Both Sets Combine gold + consecutive 4 Join with Users Get name and mail Candidate Tracking User Gold Consec Daniel 3 OK Alice 1 OK Bob 0 NO FINAL RESULT Qualified Candidates D Daniel d@mail.com 3 Gold + Consecutive A Alice a@mail.com Consecutive Medals SQL Output SELECT name, mail FROM Users WHERE user_id IN ( gold_winners UNION consecutive_winners ) Key Insight: Use UNION to combine two separate qualification criteria: (1) Users with 3+ gold medals across any contests, and (2) Users who won any medal in 3+ consecutive contests. The consecutive check uses LAG/LEAD window functions or self-joins to detect sequential contest_ids. UNION automatically removes duplicates. TutorialsPoint - Find Interview Candidates | Optimal Solution
Asked in
Meta 12 Google 8 Amazon 6
28.5K Views
Medium Frequency
~18 min Avg. Time
892 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