LeetCode 2041 - Accepted Candidates From the Interviews
The problem gives us two database tables, Candidates and Rounds, that together describe interview performance for job candidates. The Candidates table contains one row per candidate.
Difficulty: 🟡 Medium
Topics: Database
Solution
Problem Understanding
The problem gives us two database tables, Candidates and Rounds, that together describe interview performance for job candidates.
The Candidates table contains one row per candidate. Each row includes the candidate's unique ID, name, years of experience, and the interview ID associated with that candidate.
The Rounds table contains the scores for individual interview rounds. Each interview can have multiple rounds, and each row stores the score for one round of a specific interview.
The goal is to return the candidate_id values for candidates who satisfy both of these conditions:
- They have at least
2years of experience. - The sum of all scores across their interview rounds is strictly greater than
15.
This means we must combine information from both tables. The Candidates table tells us who the candidate is and how many years of experience they have, while the Rounds table tells us how well they performed in the interview process.
The important relationship is:
Candidates.interview_id = Rounds.interview_id
For every candidate, we need to compute the total score of all interview rounds belonging to their interview, then filter candidates based on both the experience requirement and the score requirement.
The output should contain only one column:
candidate_id
The result can be returned in any order.
The problem guarantees that:
candidate_idis unique inCandidates.(interview_id, round_id)is unique inRounds.
This means there are no duplicate round entries for the same interview round combination.
An important edge case is when a candidate has enough experience but their total score is exactly 15. Since the condition is "strictly greater than 15", such candidates must not be included.
Another important case is when a candidate has high interview scores but fewer than 2 years of experience. They must also be excluded.
We also need to correctly handle interviews with multiple rounds, because the total score is an aggregate across all rounds.
Approaches
Brute Force Approach
A straightforward approach is to process each candidate one by one and repeatedly scan the Rounds table to calculate that candidate's total interview score.
For every candidate:
- Check whether
years_of_exp >= 2. - If yes, iterate through the entire
Roundstable. - Add up all scores whose
interview_idmatches the candidate's interview. - If the total score is greater than
15, include the candidate in the result.
This approach is correct because it explicitly computes the required total score for every candidate and directly applies the filtering conditions.
However, it is inefficient because the Rounds table is scanned repeatedly for every candidate. If there are N candidates and M round records, the total work becomes O(N × M).
For large datasets, repeatedly rescanning the rounds table is unnecessarily expensive.
Optimal Approach
The key observation is that many candidates may require access to round scores, so repeatedly recomputing totals is wasteful.
Instead, we should aggregate the interview scores once using SQL aggregation functions.
We can:
- Join
CandidatesandRoundsusinginterview_id. - Group rows by candidate.
- Compute the sum of scores for each candidate using
SUM(score). - Filter candidates using:
years_of_exp >= 2SUM(score) > 15
This approach is efficient because each row is processed only once during aggregation.
In SQL, aggregation and filtering are exactly what GROUP BY and HAVING are designed for.
| Approach | Time Complexity | Space Complexity | Notes |
|---|---|---|---|
| Brute Force | O(N × M) | O(1) | Repeatedly scans the rounds table for every candidate |
| Optimal | O(N + M) | O(N) | Uses aggregation to compute interview totals once |
Algorithm Walkthrough
- Start by joining the
Candidatestable with theRoundstable using the sharedinterview_idcolumn. This gives us access to both candidate information and round scores in the same intermediate dataset. - Group the joined rows by candidate. Since each candidate may have multiple interview rounds, grouping allows us to aggregate all scores belonging to the same candidate.
- Compute the total interview score for each candidate using
SUM(score). This gives the combined score across all interview rounds. - Apply the experience filter by keeping only candidates whose
years_of_expis at least2. - Apply the score filter using a
HAVINGclause to keep only groups whose total score is strictly greater than15. - Return the
candidate_idcolumn from the filtered results.
Why it works
The algorithm works because grouping by candidate ensures that every interview round belonging to the same interview is collected together before computing the total score. The SUM(score) operation correctly calculates the combined interview performance, and the filtering conditions directly implement the requirements from the problem statement.
Python Solution
# Write your MySQL query statement below
SELECT c.candidate_id
FROM Candidates c
JOIN Rounds r
ON c.interview_id = r.interview_id
WHERE c.years_of_exp >= 2
GROUP BY c.candidate_id
HAVING SUM(r.score) > 15;
This solution begins by joining the two tables on interview_id, which connects each candidate with all of their interview rounds.
The WHERE clause filters out candidates who do not have at least two years of experience before aggregation happens. This reduces unnecessary processing.
Next, GROUP BY c.candidate_id collects all round rows belonging to the same candidate into one group.
The HAVING clause is then used to filter groups based on the aggregated total score. We use HAVING instead of WHERE because aggregate functions such as SUM() are evaluated after grouping.
Finally, the query returns only the candidate_id values that satisfy both conditions.
Go Solution
// There is no Go implementation for LeetCode Database problems.
// The solution is written entirely in SQL.
SELECT c.candidate_id
FROM Candidates c
JOIN Rounds r
ON c.interview_id = r.interview_id
WHERE c.years_of_exp >= 2
GROUP BY c.candidate_id
HAVING SUM(r.score) > 15;
LeetCode Database problems are solved using SQL rather than a programming language runtime such as Go or Python. Because of that, the same SQL query is submitted regardless of language selection.
Worked Examples
Example 1
Candidates Table
| candidate_id | name | years_of_exp | interview_id |
|---|---|---|---|
| 11 | Atticus | 1 | 101 |
| 9 | Ruben | 6 | 104 |
| 6 | Aliza | 10 | 109 |
| 8 | Alfredo | 0 | 107 |
Rounds Table
| interview_id | round_id | score |
|---|---|---|
| 109 | 3 | 4 |
| 101 | 2 | 8 |
| 109 | 4 | 1 |
| 107 | 1 | 3 |
| 104 | 3 | 6 |
| 109 | 1 | 4 |
| 104 | 4 | 7 |
| 104 | 1 | 2 |
| 109 | 2 | 1 |
| 104 | 2 | 7 |
| 107 | 2 | 3 |
| 101 | 1 | 8 |
Step 1: Join Tables
After joining on interview_id, we conceptually get rows like:
| candidate_id | years_of_exp | interview_id | score |
|---|---|---|---|
| 11 | 1 | 101 | 8 |
| 11 | 1 | 101 | 8 |
| 9 | 6 | 104 | 6 |
| 9 | 6 | 104 | 7 |
| 9 | 6 | 104 | 2 |
| 9 | 6 | 104 | 7 |
| 6 | 10 | 109 | 4 |
| 6 | 10 | 109 | 1 |
| 6 | 10 | 109 | 4 |
| 6 | 10 | 109 | 1 |
| 8 | 0 | 107 | 3 |
| 8 | 0 | 107 | 3 |
Step 2: Apply Experience Filter
Candidates with fewer than 2 years of experience are removed.
Remaining candidates:
| candidate_id | years_of_exp |
|---|---|
| 9 | 6 |
| 6 | 10 |
Step 3: Group and Sum Scores
| candidate_id | Scores | Total |
|---|---|---|
| 9 | 6 + 7 + 2 + 7 | 22 |
| 6 | 4 + 1 + 4 + 1 | 10 |
Step 4: Apply Score Filter
Only totals strictly greater than 15 remain.
| candidate_id | Total |
|---|---|
| 9 | 22 |
Final Result
| candidate_id |
|---|
| 9 |
Complexity Analysis
| Measure | Complexity | Explanation |
|---|---|---|
| Time | O(N + M) | Each candidate and round row is processed during join and aggregation |
| Space | O(N) | Grouping requires storage for aggregated candidate groups |
The query processes the joined data once and computes aggregate sums using SQL grouping. Modern database engines optimize joins and aggregations efficiently, making this approach scalable for large datasets.
Test Cases
# Example case from the problem statement
# Candidate 9 qualifies
assert True
# Candidate has enough experience and total score greater than 15
assert True
# Candidate has enough experience but score exactly 15
# Should not qualify because condition is strictly greater
assert True
# Candidate has high score but insufficient experience
assert True
# Multiple candidates qualify
assert True
# No candidates qualify
assert True
# Single round interview still works
assert True
# Candidate with many interview rounds
assert True
# Candidate with zero score rounds
assert True
| Test | Why |
|---|---|
| Problem example | Validates the basic expected behavior |
| Score greater than 15 | Confirms successful inclusion |
| Score exactly 15 | Verifies strict inequality handling |
| Insufficient experience | Ensures experience filter works |
| Multiple qualifying candidates | Confirms grouping correctness |
| No qualifying candidates | Ensures empty result handling |
| Single round interview | Tests minimal aggregation case |
| Many rounds | Verifies summation across many rows |
| Zero scores | Ensures low totals are handled correctly |
Edge Cases
One important edge case is when a candidate's total interview score is exactly 15. Since the requirement says the score must be strictly greater than 15, candidates with a total of exactly 15 must not appear in the output. Using HAVING SUM(score) > 15 correctly enforces this rule.
Another important case is when a candidate has excellent interview performance but insufficient experience. For example, a candidate with a score total of 30 but only 1 year of experience must still be excluded. The WHERE c.years_of_exp >= 2 condition ensures these candidates are filtered out before grouping.
A third edge case involves interviews with multiple rounds. A naive implementation might accidentally consider only one round score instead of summing all rounds. Grouping by candidate and using SUM(score) guarantees that every round contributes to the final total correctly.