Analyze Organization Hierarchy - Problem
Analyze Organization Hierarchy
You're tasked with analyzing a company's organizational structure from an employee database. Given an
๐ข The Challenge:
1. Hierarchy Levels: Determine each employee's level in the organization (CEO at level 1, direct reports at level 2, etc.)
2. Team Size: For managers, count all employees under them (direct and indirect reports)
3. Salary Budget: For managers, calculate total salary budget they control (including their own salary)
๐ Table Structure:
๐ฏ Output: Return results ordered by level (ascending), then budget (descending), then employee name (ascending).
You're tasked with analyzing a company's organizational structure from an employee database. Given an
Employees table containing employee information including their manager relationships, you need to calculate three key metrics for organizational analysis:๐ข The Challenge:
1. Hierarchy Levels: Determine each employee's level in the organization (CEO at level 1, direct reports at level 2, etc.)
2. Team Size: For managers, count all employees under them (direct and indirect reports)
3. Salary Budget: For managers, calculate total salary budget they control (including their own salary)
๐ Table Structure:
Employees table contains: employee_id, employee_name, manager_id (null for CEO), salary, and department.๐ฏ Output: Return results ordered by level (ascending), then budget (descending), then employee name (ascending).
Input & Output
example_1.sql โ Basic Hierarchy
$
Input:
Employees table:
| employee_id | employee_name | manager_id | salary | department |
|-------------|---------------|------------|--------|-----------|
| 1 | Alice | null | 100000 | Executive |
| 2 | Bob | 1 | 80000 | Engineering|
| 3 | Carol | 1 | 90000 | Marketing |
| 4 | David | 2 | 70000 | Engineering|
| 5 | Eve | 2 | 70000 | Engineering|
โบ
Output:
| employee_id | employee_name | level | team_size | budget |
|-------------|---------------|--------|-----------|--------|
| 1 | Alice | 1 | 4 | 410000 |
| 2 | Bob | 2 | 2 | 220000 |
| 3 | Carol | 2 | 0 | 90000 |
| 4 | David | 3 | 0 | 70000 |
| 5 | Eve | 3 | 0 | 70000 |
๐ก Note:
Alice is CEO (level 1) managing 4 people with total budget 410000. Bob is level 2 managing David and Eve with budget 220000. Carol has no direct reports. Results ordered by level, then budget desc, then name.
example_2.sql โ Complex Hierarchy
$
Input:
Employees table:
| employee_id | employee_name | manager_id | salary | department |
|-------------|---------------|------------|--------|-----------|
| 1 | CEO | null | 200000 | Executive |
| 2 | VP_Eng | 1 | 150000 | Engineering|
| 3 | VP_Sales | 1 | 150000 | Sales |
| 4 | TechLead | 2 | 120000 | Engineering|
| 5 | Engineer1 | 4 | 100000 | Engineering|
| 6 | Engineer2 | 4 | 100000 | Engineering|
| 7 | SalesManager | 3 | 110000 | Sales |
โบ
Output:
| employee_id | employee_name | level | team_size | budget |
|-------------|---------------|--------|-----------|--------|
| 1 | CEO | 1 | 6 | 930000 |
| 2 | VP_Eng | 2 | 3 | 470000 |
| 3 | VP_Sales | 2 | 1 | 260000 |
| 4 | TechLead | 3 | 2 | 320000 |
| 7 | SalesManager | 3 | 0 | 110000 |
| 5 | Engineer1 | 4 | 0 | 100000 |
| 6 | Engineer2 | 4 | 0 | 100000 |
๐ก Note:
Multi-level hierarchy with CEO controlling 6 people and budget of 930000. VP_Eng has larger budget than VP_Sales so appears first at level 2. TechLead manages 2 engineers at level 3.
example_3.sql โ Single Employee
$
Input:
Employees table:
| employee_id | employee_name | manager_id | salary | department |
|-------------|---------------|------------|--------|-----------|
| 1 | Solo | null | 150000 | All |
โบ
Output:
| employee_id | employee_name | level | team_size | budget |
|-------------|---------------|--------|-----------|--------|
| 1 | Solo | 1 | 0 | 150000 |
๐ก Note:
Edge case with single employee who is CEO with no subordinates. Team size is 0, budget equals their own salary.
Visualization
Tap to expand
Understanding the Visualization
1
Build Hierarchy Tree
Create recursive organizational structure starting from CEO
2
Assign Path Identifiers
Generate unique path strings for each employee's position in hierarchy
3
Calculate Team Sizes
Count subordinates by matching path patterns for each manager
4
Compute Salary Budgets
Sum salaries for all employees in each manager's subtree
5
Apply Final Ordering
Sort results by level, budget, and name as specified
Key Takeaway
๐ฏ Key Insight: Using hierarchical paths like '/1/2/3/' allows efficient pattern matching to identify all subordinates in a single database operation, making complex organizational analysis both simple and performant.
Time & Space Complexity
Time Complexity
O(n log n)
Single hierarchy traversal plus sorting for final result ordering
โก Linearithmic
Space Complexity
O(n)
Storage for hierarchy paths and intermediate window function results
โก Linearithmic Space
Constraints
- 1 โค number of employees โค 104
- 1 โค employee_id โค 106
- 1 โค salary โค 106
- Exactly one employee has manager_id = null (the CEO)
- No circular references in the management hierarchy
- employee_name length โค 50 characters
- department name length โค 30 characters
๐ก
Explanation
AI Ready
๐ก Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code