Total Importance of an Employee and All Their Reports
Last updated: November 29, 2026
Quick Overview
Each employee record has an id, an importance score, and a list of direct report ids. Given an employee id, return the sum of importance for that employee and everyone below them in the reporting tree.
0
0
0 solved
Each employee record has an id, an importance score, and a list of direct report ids. Given an employee id, return the sum of importance for that employee and everyone below them in the reporting tree.
A short technical screen question built around an org chart, the kind of data behind a people directory. It checks that you index records by id first, then do a DFS or BFS, and that you think about what happens if the data is not a clean tree.
What the Interviewer Expects
- Index employees by id in a hash map for O(1) lookup
- Traverse the subtree with DFS or BFS
- Sum importance across all visited employees
- Guard against cycles or shared reports in dirty data
- State complexity in terms of subtree size
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you answer this for every employee at once?
- What if the org data has a cycle due to a bad import?
- How would you support frequent manager changes and many queries?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Input: list of
(id, importance, reports)tuples and a queryid. - Return the subtree sum including the employee.
- Ids are unique; assume the structure is a tree but defend against a cycle....
High-Level Design
Build by_id from the list so each lookup is constant time, then run an iterative DFS from the query id. A visited set costs little and prevents double counting if one person accidentally appears und...