Problem
Table: Project
+-------------+---------+
| Column Name | Type |
+-------------+---------+
| project_id | int |
| employee_id | int |
+-------------+---------+
(project_id, employee_id) is the primary key (combination of columns with unique values) of this table.
employee_id is a foreign key (reference column) to Employee table.
Each row of this table indicates that the employee with employee_id is working on the project with project_id.
Table: Employee
+------------------+---------+
| Column Name | Type |
+------------------+---------+
| employee_id | int |
| name | varchar |
| experience_years | int |
+------------------+---------+
employee_id is the primary key (column with unique values) of this table.
Each row of this table contains information about one employee.
Write a solution to report all the projects that have the most employees.
Return the result table in any order.
The result format is in the following example.
Examples
Example 1:
|
|
Solution
Method 1 – Group By and Filter by Max Count
Intuition
We need to find the project(s) with the most employees. This is a group-by-count-max problem, solved by grouping and filtering for the maximum count.
Approach
- Group the
Project
table byproject_id
and count the number of employees per project. - Find the maximum count.
- Select the project(s) whose count equals the maximum.
Code
|
|
|
|
|
|
Complexity
- ⏰ Time complexity:
O(N)
where N is the number of rows in Project. - 🧺 Space complexity:
O(P)
where P is the number of projects.