Employee Importance Calculator

Imagine you're building a human resources system that needs to calculate the total impact of an employee within a company hierarchy. You have access to a company's organizational structure where each employee has:

• A unique ID
• An importance value (representing their individual contribution)
• A list of subordinates (employees who report directly to them)

Your Mission: Given a specific employee's ID, calculate the total importance value of that employee plus all their direct and indirect subordinates (their entire sub-organization tree).

Example: If Manager Alice (importance: 5) oversees Bob (importance: 3) and Carol (importance: 2), and Bob oversees Dave (importance: 1), then Alice's total importance would be 5 + 3 + 2 + 1 = 11.

This is essentially a tree traversal problem where you need to sum values across an entire subtree starting from a given node.

Input & Output

example_1.py — Basic Hierarchy
$ Input: employees = [[1,5,[2,3]], [2,3,[]], [3,3,[]]], id = 1
Output: 11
💡 Note: Employee 1 has importance 5, and has two direct subordinates: employee 2 and employee 3. Both employee 2 and employee 3 have importance 3. So the total importance of employee 1 is 5 + 3 + 3 = 11.
example_2.py — Single Employee
$ Input: employees = [[1,2,[]]], id = 1
Output: 2
💡 Note: Employee 1 is the only employee in this company, so their total importance is just their individual importance value of 2.
example_3.py — Deep Hierarchy
$ Input: employees = [[1,15,[2]], [2,10,[3]], [3,5,[]]], id = 1
Output: 30
💡 Note: This represents a chain of command: Employee 1 (importance 15) manages Employee 2 (importance 10), who manages Employee 3 (importance 5). Total: 15 + 10 + 5 = 30.

Visualization

Tap to expand
Employee DirectoryID: 1 → CEO (Imp: 15)ID: 2 → VP (Imp: 8)ID: 3 → Mgr (Imp: 5)ID: 4 → Dev (Imp: 3)CEOImp: 15VPImp: 8MgrImp: 5DevImp: 3DFS Traversal:1. Start at CEO (15)2. Visit VP (8)3. Visit Dev (3)4. Visit Mgr (5)Total: 15+8+3+5 = 31O(1) Lookup
Understanding the Visualization
1
Build Employee Directory
Create a hash map for instant employee lookup by ID
2
Start from Target Employee
Begin DFS traversal from the specified employee
3
Traverse All Subordinates
Visit each subordinate and their subordinates recursively
4
Sum Importance Values
Accumulate importance values from entire subtree
Key Takeaway
🎯 Key Insight: Transform the employee array into a hash map for O(1) lookups, then use tree traversal (DFS/BFS) to efficiently navigate the hierarchy and calculate total importance.

Time & Space Complexity

Time Complexity
⏱️
O(n)

O(n) to build hash map + O(n) to traverse all employees in subtree

n
2n
Linear Growth
Space Complexity
O(n)

O(n) for hash map + O(h) recursion stack where h is tree height

n
2n
Linearithmic Space

Constraints

  • 1 ≤ employees.length ≤ 2000
  • 1 ≤ employees[i].id ≤ 2000
  • 0 ≤ employees[i].importance ≤ 1000
  • One employee has at most one direct leader and may have several subordinates
  • The IDs in employees[i].subordinates are valid IDs
Asked in
Google 45 Amazon 35 Facebook 28 Microsoft 22
28.5K Views
Medium Frequency
~15 min Avg. Time
1.3K 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