Analyze Organization Hierarchy - Problem
Analyze Organization Hierarchy

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
Organization Hierarchy Analysis VisualizationCEOLevel 1 | Path: /1/Budget: $930K | Team: 6VP1Level 2 | /1/2/Budget: $470K | Team: 3VP2Level 2 | /1/3/Budget: $260K | Team: 1TL/1/2/4/$320K | 2SM/1/3/7/$110K | 0E1/1/2/4/5/E2/1/2/4/6/Path-Based Calculationโ€ข CEO (/1/) controls all /1/* pathsโ€ข VP1 (/1/2/) controls /1/2/* pathsโ€ข Team size = COUNT(subordinate paths)โ€ข Budget = SUM(salaries in subtree)โšก Single query with O(n log n)
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

n
2n
โšก Linearithmic
Space Complexity
O(n)

Storage for hierarchy paths and intermediate window function results

n
2n
โšก 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
Asked in
Google 45 Amazon 38 Microsoft 32 Meta 28
78.3K Views
High Frequency
~25 min Avg. Time
1.8K 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