Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2da9bc4

Browse files
more
1 parent 6a9a4fb commit 2da9bc4

File tree

1 file changed

+81
-0
lines changed

1 file changed

+81
-0
lines changed
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Problem 3482: Analyze Organization Hierarchy
2+
# Difficulty: Hard
3+
4+
# Table: Employees
5+
# +----------------+---------+
6+
# | Column Name | Type |
7+
# +----------------+---------+
8+
# | employee_id | int |
9+
# | employee_name | varchar |
10+
# | manager_id | int |
11+
# | salary | int |
12+
# | department | varchar |
13+
# +----------------+----------+
14+
# employee_id is the unique key for this table.
15+
# Each row contains information about an employee, including their ID, name, their manager's ID, salary, and department.
16+
# manager_id is null for the top-level manager (CEO).
17+
18+
# Problem Statement:
19+
# Write a solution to analyze the organizational hierarchy and answer the following:
20+
# - Hierarchy Levels: For each employee, determine their level in the organization (CEO is level 1, employees reporting directly to the CEO are level 2, and so on).
21+
# - Team Size: For each employee who is a manager, count the total number of employees under them (direct and indirect reports).
22+
# - Salary Budget: For each manager, calculate the total salary budget they control (sum of salaries of all employees under them, including indirect reports, plus their own salary).
23+
# Return the result table ordered by the result ordered by level in ascending order, then by budget in descending order, and finally by employee_name in ascending order.
24+
25+
# Solution
26+
27+
import pandas as pd
28+
from collections import defaultdict
29+
30+
def analyze_organization_hierarchy(df: pd.DataFrame) -> pd.DataFrame:
31+
32+
def traverseGraph(mgr: int, lev: int) -> None:
33+
# I set the current manager's level
34+
levl[mgr] = lev
35+
36+
# I traverse through each employee reporting to this manager
37+
for emp in grph[mgr]:
38+
traverseGraph(emp, lev + 1)
39+
40+
# I accumulate team size and budget recursively
41+
team[mgr] += team[emp] + 1
42+
bdgt[mgr] += bdgt[emp]
43+
return
44+
45+
# I build the manager-to-employee graph
46+
grph = defaultdict(list)
47+
levl, team, bdgt = defaultdict(int), defaultdict(int), defaultdict(int)
48+
49+
# I identify the top-level manager (CEO)
50+
boss = df.loc[df.manager_id.isna(), "employee_id"].values[0]
51+
52+
# I populate the graph and initialize budgets with individual salaries
53+
for mgr, emp, sal in zip(df.manager_id, df.employee_id, df.salary):
54+
grph[mgr].append(emp)
55+
bdgt[emp] = sal
56+
57+
# I start DFS from the CEO to populate level, team size, and budget
58+
traverseGraph(boss, 1)
59+
60+
# I add the computed columns to the DataFrame
61+
df['level'] = df.employee_id.apply(lambda x: levl[x])
62+
df['team_size'] = df.employee_id.apply(lambda x: team[x])
63+
df['budget'] = df.employee_id.apply(lambda x: bdgt[x])
64+
65+
# I return the final sorted result as per requirements
66+
return df.sort_values(['level','budget','employee_name'],
67+
ascending=[1,0,1])[['employee_id', 'employee_name', 'level', 'team_size', 'budget']]
68+
69+
# Intuition:
70+
# I need to compute hierarchical information in an organization using a tree-like structure.
71+
# Each manager has direct reports who may themselves be managers, so I use DFS to calculate:
72+
# - level (distance from CEO)
73+
# - team size (number of all employees under a manager)
74+
# - budget (sum of salaries under the manager's control)
75+
76+
# Explanation:
77+
# I create a graph where each manager points to a list of their direct reports.
78+
# I initialize the `budget` dictionary with employees' own salaries and fill in team size and levels via DFS traversal.
79+
# While returning from recursion, I aggregate the budget and team size upward through the tree.
80+
# The final DataFrame gets three new columns: `level`, `team_size`, and `budget`.
81+
# I sort the DataFrame as required by level (asc), budget (desc), and employee_name (asc), and return the required columns.

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /