Identify All Projects with Maximum Team Size
Arrays & HashingAuto-graded · 23 test casesLast updated
Group assignment pairs by project, count members per project, and return every project tied for the largest team.
You are given a list of project assignments. Each assignment is a pair [project_id, employee_id] indicating that a particular employee is assigned to a particular project. The same (project_id, employee_id) pair will never appear more than once.
Your task is to identify all projects that have the largest number of assigned employees. If multiple projects are tied for the maximum team size, return all of them.
Input:
assignments: a list of pairs, where each pair is[project_id, employee_id](both integers).
Output:
- A list of project IDs (integers) that have the maximum number of employees, sorted in ascending order.
- If the input is empty, return an empty list.
Constraints:
0 ≤ len(assignments) ≤ 100,0001 ≤ project_id, employee_id ≤ 1,000,000- No duplicate
(project_id, employee_id)pairs will appear in the input. - Project IDs may be non-contiguous and in any order.
Examples
Example 1
Input: projects_with_max_team_size([[101, 1], [101, 2], [202, 3]])
Output: [101]
Example 2
Input: projects_with_max_team_size([[8, 1], [8, 2], [3, 4], [3, 5]])
Output: [3, 8]
Example 3
Input: projects_with_max_team_size([])
Output: []
Hints
Hint 1
Count how many times each project_id appears in the assignment list—that gives you the team size for each project.
Hint 2
Find the maximum team size across all projects, then filter to keep only those projects that match that maximum.
Hint 3
Handle the empty input case explicitly: if there are no assignments, there is no maximum, so return an empty list.
Worked solution
Medium Try it yourself first — then compare your approach, complexity and edge cases.
Approach
The problem requires three steps: count employees per project, find the maximum count, and return all projects that match that maximum.
Step 1: Group and count. Each assignment pair represents one employee on one project. Since the input guarantees no duplicate pairs, we can simply count how many times each project_id appears. A Counter from the collections module is perfect for this: iterate through the assignments, extract the project_id from each pair, and let the counter accumulate the frequencies.
Step 2: Find the maximum. Once we have the counts, take the maximum value. This tells us the size of the largest team.
Step 3: Filter and sort. Loop through the counter and keep every project whose count equals the maximum. Sort the result in ascending order to produce a deterministic output.
Edge case: empty input. If there are no assignments, the counter is empty and calling max() on an empty sequence would raise a ValueError. We guard against this by checking whether the counter is empty and returning an empty list immediately.
Why It Works
- Counting: The guarantee that no
(project_id, employee_id)pair appears twice means each occurrence of aproject_idrepresents a distinct employee. Counting occurrences gives the exact team size. - Ties: We compare each project's count against the maximum using equality, so every project that reaches the maximum is included. This is different from approaches that use
LIMIT 1or pick an arbitrary winner; those would fail when multiple projects are tied. - Ordering: Sorting the result by
project_idensures the output is consistent and matches the expected format.
Complexity
Let n be the number of assignment pairs and p be the number of distinct projects.
Time: O(n) to build the counter, O(p) to find the maximum and filter, and O(k log k) to sort the k projects that match the maximum. In the worst case, all projects are tied and k = p, giving O(n + p log p).
Space: O(p) for the counter that stores one entry per project.
Solution
from collections import Counter
from typing import List
def projects_with_max_team_size(assignments: List[List[int]]) -> List[int]:
counts = Counter(project_id for project_id, employee_id in assignments)
if not counts:
return []
highest = max(counts.values())
return sorted(project_id for project_id, count in counts.items() if count == highest)