Practice Analyze Organization Hierarchy
Read the task, work through the examples, and check your approach against the constraints. Use the original LeetCode task for coding and submissions.
Problem Statement
Employees has columns employee_id (unique integer), employee_name (text), manager_id (integer, null for the CEO), salary (integer), and department (text). Return every employee's employee_id, employee_name, level, team_size, and budget. The CEO has level 1; each reporting step adds one. team_size counts all direct and indirect reports, excluding the employee. budget includes the employee's own salary and every descendant's salary. Sort by level ascending, budget descending, then employee_name ascending. Practice approach: Use a recursive CTE to derive levels from the root, and an ancestor-descendant relation to aggregate each employee's full subtree. Include a self-row for salary aggregation but exclude it from the report count. Preserve leaf employees with team_size zero. Check that joins do not multiply salaries.
Source: Analyze Organization Hierarchy on LeetCode. Use the original task for its coding environment and submissions.
Examples
# Example 1
Employees (employee_id, employee_name, manager_id, salary, department): (1, "Mira", null, 100, "Executive") (2, "Noah", 1, 70, "Sales") (3, "Omar", 1, 80, "Engineering") (4, "Pia", 2, 40, "Sales")
employee_id, employee_name, level, team_size, budget 1, Mira, 1, 3, 290 2, Noah, 2, 1, 110 3, Omar, 2, 0, 80 4, Pia, 3, 0, 40
Mira controls every salary. Noah controls his own 70 plus Pia's 40; leaves have no reports and keep their own salary as budget.
Related LeetCode Problems
Practice more problems similar to Analyze Organization Hierarchy.
Review your practice with PhantomCodeAI
After trying the problem yourself, use the app to discuss your approach, complexity, and edge cases.