Skip to main content

Accepted Candidates From the Interviews - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase5 min read
Practice this problem

Problem Statement

Table: Candidates

+--------------+----------+
| Column Name  | Type     |
+--------------+----------+
| candidate_id | int      |
| name         | varchar  |
| years_of_exp | int      |
| interview_id | int      |
+--------------+----------+
candidate_id is the primary key (column with unique values) for this table.
Each row of this table indicates the name of a candidate, their number of years of experience, and their interview ID.

 

Table: Rounds

+--------------+------+
| Column Name  | Type |
+--------------+------+
| interview_id | int  |
| round_id     | int  |
| score        | int  |
+--------------+------+
(interview_id, round_id) is the primary key (combination of columns with unique values) for this table.
Each row of this table indicates the score of one round of an interview.

 

Write a solution to report the IDs of the candidates who have at least two years of experience and the sum of the score of their interview rounds is strictly greater than 15.

Return the result table in any order.

The result format is in the following example.

 

Example 1:

Input: 
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     |
+--------------+----------+-------+
Output: 
+--------------+
| candidate_id |
+--------------+
| 9            |
+--------------+
Explanation: 
- Candidate 11: The total score is 16, and they have one year of experience. We do not include them in the result table because of their years of experience.
- Candidate 9: The total score is 22, and they have six years of experience. We include them in the result table.
- Candidate 6: The total score is 10, and they have ten years of experience. We do not include them in the result table because the score is not good enough.
- Candidate 8: The total score is 6, and they have zero years of experience. We do not include them in the result table because of their years of experience and the score.

Approach Overview

Problem Overview: You need to identify candidates who successfully pass the interview process. Each candidate participates in multiple interview rounds with recorded scores. A candidate is considered accepted only if they pass every required round, which requires joining interview data and validating scores across all rounds.

Approach 1: Join Tables + Grouping + Filtering (O(n log n) time, O(n) space)

The key idea is to combine candidate information with their interview results and then evaluate performance across all rounds. Start by joining the candidate table with the interview results table using the candidate identifier. This produces a dataset containing every candidate and their scores for each round. SQL JOIN operations allow you to merge these records efficiently before aggregation.

Next, aggregate results by candidate using GROUP BY candidate_id. Aggregation enables you to evaluate performance across multiple rounds for the same candidate. Functions such as COUNT() determine how many rounds a candidate attended, while MIN(score) or similar checks ensure the candidate passed every round. If the lowest score across all rounds still meets the passing threshold, the candidate did not fail any stage.

Filtering happens in the HAVING clause. Unlike WHERE, HAVING works on aggregated results. You can enforce rules such as passing every round or completing all required rounds by combining conditions like MIN(score) and COUNT(). This step eliminates candidates who failed at least one interview stage.

This approach relies heavily on relational operations: table joins, grouping, and aggregated filtering. These patterns appear frequently in database and SQL interview questions. The same logic can be replicated in Pandas using merge(), groupby(), and aggregation filters, which mirrors how relational queries operate in analytical workflows.

Recommended for interviews: Interviewers expect a clean relational query that joins the tables, groups by candidate, and filters with HAVING. A brute-force row-by-row check would show basic understanding, but the grouped SQL solution demonstrates strong command of relational aggregation and is the standard approach for database interview problems.

Solution

We can join the Candidates table and the Rounds table based on interview_id, filter out candidates with at least 2 years of work experience, then group by candidate_id to calculate the total score for each candidate, and finally filter out candidates with a total score greater than 15.

Code

MySQL

Pandas

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Join + Group By + HAVINGO(n log n)O(n)Standard relational query when validating conditions across multiple interview rounds
Join + Aggregation in PandasO(n log n)O(n)Data analysis workflows where interview data is processed in Python using DataFrames

Video Solution

LeetCode Medium 2041 Interview SQL Question with Detailed Explanation • Everyday Data Science • 2,974 views views

Frequently Asked Questions

Is Accepted Candidates From the Interviews easy or hard?
The problem is classified as Medium because it requires understanding how to combine multiple SQL operations. The logic itself is straightforward, but correctly using JOIN, GROUP BY, and HAVING together is a common challenge for candidates learning database queries.
Accepted Candidates From the Interviews Python/Java solution
In SQL (MySQL), the solution uses JOIN, GROUP BY, and HAVING filters. In Python with Pandas, you merge the tables using merge(), group records using groupby(candidate_id), compute aggregated metrics such as minimum score and round counts, and filter rows that satisfy the acceptance conditions.
How to solve Accepted Candidates From the Interviews in O(n)?
Conceptually the task can be processed in linear time by scanning interview records and maintaining aggregated statistics per candidate, such as minimum score and round count. In SQL, this is implemented through GROUP BY aggregation which databases internally optimize close to linear scans depending on indexing.
What is the best approach for Accepted Candidates From the Interviews?
The most effective solution joins the candidate and interview tables, then groups results by candidate_id. Aggregation functions such as COUNT() and MIN(score) verify that each candidate completed and passed every interview round. A HAVING clause filters out candidates who failed any stage.
Is Accepted Candidates From the Interviews asked at Google/Amazon/Meta?
Database aggregation and filtering problems like this frequently appear in SQL interviews at companies such as Amazon, Meta, and other data-heavy organizations. The focus is usually on joins, grouping, and HAVING conditions to validate constraints across related tables.
What data structure is used in Accepted Candidates From the Interviews?
The problem relies on relational tables and aggregation logic rather than traditional algorithmic data structures. Internally, database engines often use hash tables or sorting structures to implement GROUP BY operations efficiently.
What is the time complexity of Accepted Candidates From the Interviews?
The SQL solution typically runs in O(n log n) time due to grouping and aggregation operations performed by the database engine. Space complexity is O(n) for intermediate grouped results depending on query execution and indexing.

Ready to solve this problem?

Practice Accepted Candidates From the Interviews with our built-in code editor and test cases.

Practice on FleetCode