AlgoMaster Logo

Employee Importance

mediumFrequency6 min readUpdated June 23, 2026

Understanding the Problem

We are given a flat list of employees, each with an ID, an importance value, and a list of their direct subordinate IDs. Given a target employee ID, we need to compute the total importance of that employee plus every employee underneath them in the hierarchy, no matter how many levels deep.

The employee structure forms a tree (or more precisely, a forest, but we only care about the subtree rooted at the target). The complication is that employees arrive as a flat list, not as linked tree nodes. A subordinate is referenced only by ID, so before we can move from an employee to their subordinates, we need a way to resolve any ID to its employee object.

The problem therefore has two parts: build a fast lookup from employee ID to employee object, then traverse the subtree rooted at the target employee, accumulating importance values along the way.

Key Constraints:

  • 1 <= employees.length <= 2000 -> Even rescanning the list for every ID lookup, O(n^2) overall, would pass at this size, but a hash map gives O(n) for the same amount of code.
  • -100 <= importance <= 100 -> Importance values are small. Even summing all 2000 employees at their maximum gives 2000 * 100 = 200,000, well within 32-bit integer range. No overflow concerns here.
  • No cycles in subordination -> The structure is a tree, so we do not need cycle detection or a visited set during traversal.
  • id is guaranteed valid -> No need to handle "employee not found" errors.

Approach 1: DFS (Recursive)

Intuition

The total importance of an employee equals their own importance plus the total importance of each of their direct subordinates. That definition is already recursive: each subordinate's total is computed the same way, and the recursion bottoms out at employees with no subordinates.

Before we can traverse, we need a way to jump from a subordinate ID to the actual employee object. The input gives us a list, and scanning it for every lookup would cost O(n) each time. So we first build a hash map from employee ID to employee object. After that, the DFS is short: look up the target, add their importance, recurse on each subordinate ID.

Algorithm

  1. Build a hash map that maps each employee's ID to their employee object.
  2. Define a recursive function dfs(id) that:
    • Looks up the employee in the map.
    • Starts with this employee's importance value.
    • For each subordinate ID, recursively calls dfs and adds the returned value.
    • Returns the total.
  3. Call dfs with the given target ID and return the result.

Example Walkthrough

Trace employees = [[1, 5, [2, 3]], [2, 3, [4]], [3, 4, []], [4, 1, []]] with id = 1. Employee 4 is an indirect subordinate of employee 1, reachable only through employee 2, so the recursion has to go two levels deep on that branch.

1Build map: {1: Emp(5,[2,3]), 2: Emp(3,[4]), 3: Emp(4,[]), 4: Emp(1,[])}
1
:
imp=5, subs=[2,3]
2
:
imp=3, subs=[4]
3
:
imp=4, subs=[]
4
:
imp=1, subs=[]
1/7

Code

Recursive DFS is optimal in time, but it spends call-stack space proportional to the depth of the hierarchy, and a long management chain can overflow the stack. The next approach replaces the implicit call stack with an explicit queue.

Approach 2: BFS (Iterative)

Intuition

Instead of recursing into subordinates, we can use a queue to process employees iteratively. Start by adding the target employee's ID to the queue. Then, while the queue is not empty, dequeue an ID, look up the employee, add their importance to a running total, and enqueue all their subordinate IDs. This explores the entire subtree without any risk of stack overflow.

BFS and DFS visit the same set of employees in a different order. BFS processes the target first, then all direct subordinates, then all employees two levels down, and so on. The total importance ends up the same regardless of traversal order because addition is commutative.

Algorithm

  1. Build a hash map that maps each employee's ID to their employee object.
  2. Initialize a queue with the target employee's ID.
  3. Initialize a total importance counter to 0.
  4. While the queue is not empty:
    • Dequeue an employee ID.
    • Look up the employee in the map.
    • Add their importance to the total.
    • Enqueue all their subordinate IDs.
  5. Return the total.

Example Walkthrough

The same input as before: employees = [[1, 5, [2, 3]], [2, 3, [4]], [3, 4, []], [4, 1, []]] with id = 1. BFS reaches employee 3 before employee 4, the reverse of the DFS order, and still arrives at the same total of 13.

1Initialize: queue=[1], total=0
Front
1
Rear
1/6

Code