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 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
Company Reporting StructureCEOID: 1VP1ID: 2VP2ID: 3VP3ID: 4Level 1D1ID: 5D2ID: 6D3ID: 7D4ID: 8Level 2M1ID: 9M2ID: 10M3ID: 11Level 3Result SetAll Subordinates:[2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
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

n
2n
โœ“ Linear Growth
Space Complexity
O(d)

Space complexity is O(d) where d is the maximum depth of the hierarchy (recursion stack)

n
2n
โœ“ 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
Asked in
Amazon 45 Microsoft 38 Google 32 Meta 28
24.7K Views
Medium Frequency
~15 min Avg. Time
892 Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen