Find Candidates for Data Scientist Position II Problem
Description
LeetCode Problem 3278.
Table: Candidates
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| candidate_id | int |
| skill | varchar |
| proficiency | int |
+--------------+---------+
(candidate_id, skill) is the unique key for this table.
Each row includes candidate_id, skill, and proficiency level (1-5).
Table: Projects
+--------------+---------+
| Column Name | Type |
+--------------+---------+
| project_id | int |
| skill | varchar |
| importance | int |
+--------------+---------+
(project_id, skill) is the primary key for this table.
Each row includes project_id, required skill, and its importance (1-5) for the project.
Leetcode is staffing for multiple data science projects. Write a solution to find the best candidate for each project based on the following criteria:
- Candidates must have all the skills required for a project.
- Calculate a score for each candidate-project pair as follows:
- Start with
100points - Add
10points for each skill where proficiency > importance - Subtract
5points for each skill where proficiency < importance - If the candidate's skill proficiency equal to the project's skill importance, the score remains unchanged
- Start with
Include only the top candidate (highest score) for each project. If there’s a tie, choose the candidate with the lower candidate_id. If there is no suitable candidate for a project, do not return that project.
Return a result table ordered by project_id in ascending order.
The result format is in the following example.
Example:
Input:
Candidates table:
+--------------+-----------+-------------+
| candidate_id | skill | proficiency |
+--------------+-----------+-------------+
| 101 | Python | 5 |
| 101 | Tableau | 3 |
| 101 | PostgreSQL| 4 |
| 101 | TensorFlow| 2 |
| 102 | Python | 4 |
| 102 | Tableau | 5 |
| 102 | PostgreSQL| 4 |
| 102 | R | 4 |
| 103 | Python | 3 |
| 103 | Tableau | 5 |
| 103 | PostgreSQL| 5 |
| 103 | Spark | 4 |
+--------------+-----------+-------------+
Projects table:
+-------------+-----------+------------+
| project_id | skill | importance |
+-------------+-----------+------------+
| 501 | Python | 4 |
| 501 | Tableau | 3 |
| 501 | PostgreSQL| 5 |
| 502 | Python | 3 |
| 502 | Tableau | 4 |
| 502 | R | 2 |
+-------------+-----------+------------+
Output:
+-------------+--------------+-------+
| project_id | candidate_id | score |
+-------------+--------------+-------+
| 501 | 101 | 105 |
| 502 | 102 | 130 |
+-------------+--------------+-------+
Explanation:
- For Project 501, Candidate 101 has the highest score of 105. All other candidates have the same score but Candidate 101 has the lowest candidate_id among them.
- For Project 502, Candidate 102 has the highest score of 130.
The output table is ordered by project_id in ascending order.
Solution
Approach: Join on skill, then rank
A candidate qualifies for a project only if they have every skill the project asks for.
- Join
CandidatestoProjectsonskill. Each joined row is one required skill that the candidate has, so grouping by(project_id, candidate_id)gives the number of matched skills and, with aCASE, the score: start at 100, then add 10 whereproficiency > importanceand subtract 5 whereproficiency < importance. - Count the skills each project requires. Keep only the pairs whose matched count equals that number; this removes candidates who are missing a skill.
- Rank the survivors inside each project by
score DESC, candidate_id ASCand keep rank 1. A project with no qualified candidate has no rows after step 2, so it disappears from the answer automatically.
The tie-break on candidate_id inside the window makes the rank unique, so ROW_NUMBER and RANK behave the same here. The Pandas version follows the same steps with merge, groupby and a sort that keeps the first row per project.
WITH matched AS (
-- one row per (project, candidate) with the skills they share and the score
SELECT
p.project_id,
c.candidate_id,
COUNT(*) AS matched_skills,
100 + SUM(
CASE
WHEN c.proficiency > p.importance THEN 10
WHEN c.proficiency < p.importance THEN -5
ELSE 0
END
) AS score
FROM Candidates AS c
JOIN Projects AS p
ON p.skill = c.skill
GROUP BY p.project_id, c.candidate_id
),
required AS (
SELECT project_id, COUNT(*) AS required_skills
FROM Projects
GROUP BY project_id
),
ranked AS (
SELECT
m.project_id,
m.candidate_id,
m.score,
RANK() OVER (
PARTITION BY m.project_id
ORDER BY m.score DESC, m.candidate_id
) AS rk
FROM matched AS m
JOIN required AS r
ON r.project_id = m.project_id
AND r.required_skills = m.matched_skills
)
SELECT project_id, candidate_id, score
FROM ranked
WHERE rk = 1
ORDER BY project_id;