All People Report to the Given Manager - Problem
Company Hierarchy Reporting Structure
You're working with a company's employee database that tracks the reporting structure. The database contains information about employees and their direct managers, forming a hierarchical tree structure.
Given an
The company guarantees that the reporting chain won't exceed 3 levels deep, making this a manageable tree traversal problem.
Goal: Return the employee IDs of all people who report to the head of the company.
Table Structure:
+---------------+---------+
| Column Name | Type |
+---------------+---------+
| employee_id | int |
| employee_name | varchar |
| manager_id | int |
+---------------+---------+
You're working with a company's employee database that tracks the reporting structure. The database contains information about employees and their direct managers, forming a hierarchical tree structure.
Given an
Employees table where each employee has an ID, name, and manager ID, your task is to find all employees who report to the company head (employee with ID = 1), either directly or indirectly.The company guarantees that the reporting chain won't exceed 3 levels deep, making this a manageable tree traversal problem.
Goal: Return the employee IDs of all people who report to the head of the company.
Table Structure:
Employees+---------------+---------+
| Column Name | Type |
+---------------+---------+
| employee_id | int |
| employee_name | varchar |
| manager_id | int |
+---------------+---------+
Input & Output
example_1.sql โ Basic Hierarchy
$
Input:
Employees table:
+-------------+---------------+------------+
| employee_id | employee_name | manager_id |
+-------------+---------------+------------+
| 1 | Boss | 1 |
| 3 | Alice | 3 |
| 2 | Bob | 1 |
| 4 | Daniel | 2 |
| 7 | Luis | 4 |
| 8 | Jhon | 3 |
| 9 | Angela | 8 |
| 77 | Robert | 1 |
+-------------+---------------+------------+
โบ
Output:
[2, 4, 7, 8, 9, 77]
๐ก Note:
The head of the company is employee with ID 1. Direct reports: Bob(2) and Robert(77). Bob's report: Daniel(4). Daniel's report: Luis(7). Alice(3) reports to herself, so her subordinates also count: Jhon(8) and Angela(9) who reports to Jhon.
example_2.sql โ Simple Chain
$
Input:
Employees table:
+-------------+---------------+------------+
| employee_id | employee_name | manager_id |
+-------------+---------------+------------+
| 1 | CEO | 1 |
| 2 | VP | 1 |
| 3 | Director | 2 |
| 4 | Manager | 3 |
+-------------+---------------+------------+
โบ
Output:
[2, 3, 4]
๐ก Note:
Simple hierarchy chain: CEO(1) โ VP(2) โ Director(3) โ Manager(4). All employees except CEO report to the head either directly or indirectly.
example_3.sql โ Only Direct Reports
$
Input:
Employees table:
+-------------+---------------+------------+
| employee_id | employee_name | manager_id |
+-------------+---------------+------------+
| 1 | Head | 1 |
| 2 | VP1 | 1 |
| 3 | VP2 | 1 |
| 4 | VP3 | 1 |
+-------------+---------------+------------+
โบ
Output:
[2, 3, 4]
๐ก Note:
Flat structure where all VPs report directly to the head. No indirect reports exist.
Visualization
Tap to expand
Understanding the Visualization
1
Start at the Top
Begin with the head of company (employee_id = 1)
2
Find Direct Reports
Identify all employees with manager_id = 1
3
Expand the Search
For each found employee, find their direct reports
4
Continue Recursively
Keep expanding until no more subordinates are found
5
Collect Results
Return all employee IDs found in the traversal
Key Takeaway
๐ฏ Key Insight: Using recursive CTE allows us to traverse the entire hierarchy in one elegant query, making it both efficient and maintainable for any depth of organizational structure.
Time & Space Complexity
Time Complexity
O(n)
Each employee is visited exactly once during the traversal
โ Linear Growth
Space Complexity
O(d)
Space complexity is O(d) where d is the maximum depth of the hierarchy (recursion stack)
โ Linear Space
Constraints
- 1 โค n โค 1000 (number of employees)
- 1 โค employee_id โค 109
- employee_id is unique for each employee
- The head of the company has employee_id = 1
- The reporting chain will not exceed 3 levels deep
- manager_id references a valid employee_id or the employee's own ID
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code