Skip to main content

Manager of the Largest Department - Solution & Explanation

MediumPremiumFree on FleetCodeDatabase4 min readAsked at: Dassault Sysetmes
Practice this problem

Problem Statement

Table: Employees

+-------------+---------+
| Column Name | Type    |
+-------------+---------+
| emp_id      | int     |
| emp_name    | varchar |
| dep_id      | int     |
| position    | varchar |
+-------------+---------+
emp_id is column of unique values for this table.
This table contains emp_id, emp_name, dep_id, and position.

Write a solution to find the name of the manager from the largest department. There may be multiple largest departments when the number of employees in those departments is the same.

Return the result table sorted by dep_id in ascending order.

The result format is in the following example.

 

Example 1:

Input: 
Employees table:
+--------+----------+--------+---------------+
| emp_id | emp_name | dep_id | position      | 
+--------+----------+--------+---------------+
| 156    | Michael  | 107    | Manager       |
| 112    | Lucas    | 107    | Consultant    |    
| 8      | Isabella | 101    | Manager       | 
| 160    | Joseph   | 100    | Manager       | 
| 80     | Aiden    | 100    | Engineer      | 
| 190    | Skylar   | 100    | Freelancer    | 
| 196    | Stella   | 101    | Coordinator   |
| 167    | Audrey   | 100    | Consultant    |
| 97     | Nathan   | 101    | Supervisor    |
| 128    | Ian      | 101    | Administrator |
| 81     | Ethan    | 107    | Administrator |
+--------+----------+--------+---------------+
Output
+--------------+--------+
| manager_name | dep_id | 
+--------------+--------+
| Joseph       | 100    | 
| Isabella     | 101    | 
+--------------+--------+
Explanation
- Departments with IDs 100 and 101 each has a total of 4 employees, while department 107 has 3 employees. Since both departments 100 and 101 have an equal number of employees, their respective managers will be included.
Output table is ordered by dep_id in ascending order.

Approach Overview

Problem Overview: The query asks for the manager responsible for the department that has the highest number of employees. You first need to count how many employees belong to each department, then identify the department with the largest count, and finally return the manager of that department.

Approach 1: Grouping + Equi-Join + Subquery (O(n) time, O(1) extra space)

This approach relies on SQL aggregation to measure department size. Start by grouping employees by department_id and computing COUNT(*) to determine how many employees belong to each department. A subquery then finds the maximum department size using MAX() over those grouped counts. The outer query joins the department table with the grouped results using an equi-join and filters for the department whose count equals the maximum.

The key insight is separating the problem into two stages: compute department sizes, then select the largest one. SQL handles this efficiently with GROUP BY and a nested subquery. The equi-join ensures you retrieve the manager assigned to that department. This pattern is common in SQL interview questions where you must combine aggregation with relational joins.

If multiple departments tie for the largest size, the query naturally returns all matching managers because the filter compares counts against the maximum value. This makes the solution robust without additional conditional logic. The database engine performs a scan of the employee table, builds grouped counts, and evaluates the maximum in the subquery.

Conceptually, the solution combines three core database operations: aggregation (GROUP BY), relational matching via joins, and filtering with a scalar subquery. These patterns appear frequently in SQL interview problems involving "largest", "highest", or "top" entity queries.

Recommended for interviews: The grouping + subquery solution is the expected approach. It clearly demonstrates understanding of aggregation and relational joins. Variants using window functions or ordering with LIMIT may also work, but the explicit GROUP BY + MAX() subquery shows the strongest command of core SQL fundamentals.

Solution

We can first count the number of employees in each department, denoted as table T. Then we join T with the Employees table, with the join condition being T.dep_id = Employees.dep_id and Employees.position = 'Manager'. This way, we can get the manager of each department. Finally, we filter out the department with the most employees.

Code

MySQL

Try this approach in the editor →

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Grouping + Subquery + JoinO(n)O(1)Standard SQL solution for finding entities with maximum aggregated values
GROUP BY + ORDER BY + LIMITO(n log n)O(1)Useful when only the top department is required and sorting is acceptable
Window Function (RANK / DENSE_RANK)O(n)O(n)Modern SQL engines where analytic functions are preferred

Video Solution

Leetcode MEDIUM 2988 - Manager Largest Department COUNT WINDOW - Explained by Everyday Data Science • Everyday Data Science • 514 views views

Frequently Asked Questions

Is Manager of the Largest Department easy or hard?
The problem is rated Medium because it combines multiple SQL concepts in a single query: aggregation, subqueries, and joins. Each concept individually is straightforward, but combining them correctly requires a solid understanding of SQL query structure.
Manager of the Largest Department Python/Java solution
This problem is a SQL database query rather than a typical algorithm implemented in Python or Java. The solution is written directly in SQL using GROUP BY, COUNT, and a subquery that identifies the maximum department size.
How to solve Manager of the Largest Department in O(n)?
Group employees by department_id and compute COUNT(*) to get department sizes. Use a subquery to determine the maximum count across all departments. Join the department table and filter rows where the employee count equals that maximum value. The database performs a single aggregation pass over the employee table.
What is the best approach for Manager of the Largest Department?
The most common solution uses SQL aggregation with GROUP BY to count employees per department, followed by a subquery that finds the maximum count. The department table is then joined to retrieve the manager of that department. This approach runs in O(n) time relative to the number of employee rows and clearly demonstrates understanding of SQL aggregation and joins.
Is Manager of the Largest Department asked at Google/Amazon/Meta?
SQL aggregation and join problems like this are common in database interview rounds at companies such as Amazon, Meta, and Google. Variants often involve identifying the largest group, top department, or highest-performing category using GROUP BY and subqueries.
What data structure is used in Manager of the Largest Department?
In SQL terms, the query relies on relational tables combined with aggregation operations. The database engine internally builds grouped aggregates similar to hash-based grouping while executing GROUP BY. The final result is produced using an equi-join between aggregated results and the department table.
What is the time complexity of Manager of the Largest Department?
The typical SQL solution scans the employee table once to compute department counts using GROUP BY, which is O(n). The MAX subquery operates on the grouped results, which is much smaller than the original dataset. Overall complexity is effectively O(n) for most database engines.

Ready to solve this problem?

Practice Manager of the Largest Department with our built-in code editor and test cases.

Practice on FleetCode