Skip to main content
Back to problems
#3051
Easy Database

Find candidates for data scientist position

Database
75.1% acceptance
Mar 31, 2026
15
0

No description available.

Solution

Pandas
Time O(n)
Space O(1)
LeetCode
solution.pandas
# Table: Candidates
# 
# +--------------+---------+
# | Column Name  | Type    |
# +--------------+---------+
# | candidate_id | int     |
# | skill        | varchar |
# +--------------+---------+
# (candidate_id, skill) is the primary key (columns with unique values) for this table.
# Each row includes candidate_id and skill.
# 
# Write a query to find the candidates best suited for a Data Scientist position. The candidate must be proficient in Python, Tableau, and PostgreSQL.
# 
# Return the result table ordered by candidate_id in ascending order.
# 
# The result format is in the following example.
#
# Example 1:
# Input:
# Candidates table:
# +---------------+--------------+
# | candidate_id  | skill        |
# +---------------+--------------+
# | 123           | Python       |
# | 234           | R            |
# | 123           | Tableau      |
# | 123           | PostgreSQL   |
# | 234           | PowerBI      |
# | 234           | SQL Server   |
# | 147           | Python       |
# | 147           | Tableau      |
# | 147           | Java         |
# | 147           | PostgreSQL   |
# | 256           | Tableau      |
# | 102           | DataAnalysis |
# +---------------+--------------+
# Output:
# +--------------+
# | candidate_id |
# +--------------+
# | 123          |
# | 147          |
# +--------------+
# Explanation:
# - Candidates 123 and 147 possess the necessary skills in Python, Tableau, and PostgreSQL for the data scientist position.
# - Candidates 234 and 102 do not possess any of the required skills for this position.
# - Candidate 256 has proficiency in Tableau but is missing skills in Python and PostgreSQL.
# The output table is sorted by candidate_id in ascending order.

import pandas as pd

def find_candidates(candidates: pd.DataFrame) -> pd.DataFrame:
  required = {'Python', 'Tableau', 'PostgreSQL'}
  result = candidates[candidates['skill'].isin(required)].groupby('candidate_id')['skill'].nunique().reset_index()
  result = result[result['skill'] == 3][['candidate_id']].sort_values('candidate_id')
  return result