Employee Importance from a Dictionary
Updated
Problem statement
Given the employee records employees of a company and an employee id id, return the total importance of that employee plus every person who reports to them, directly or indirectly.
The records are a dictionary keyed by employee id, given as a list of its entries. Each entry is one row employees[i] = [eid, importance, r1, r2, ...]: employee eid has the importance value importance, and r1, r2, ... are the ids of the people whose direct manager is eid. A row with only two numbers belongs to someone with no direct reports. The rows can come in any order.
A person reports to id indirectly when id is their manager's manager, or further up the same chain.
Function
getImportance(employees: List<List<Integer>>, id: int) → intExamples
Example 1
employees = [[3, 4, 7], [1, 10, 3, 5], [5, -2], [7, 6], [9, 8]]id = 1return = 18Employees 3 and 5 report directly to employee 1, and employee 7 reports to 3, so 7 is an indirect report. Employee 9 is not in this chain, so the total is 10 + 4 + (-2) + 6 = 18.
Example 2
employees = [[3, 4, 7], [1, 10, 3, 5], [5, -2], [7, 6], [9, 8]]id = 3return = 10Only employee 7 is below employee 3, so the total is 4 + 6 = 10. Employees 1 and 5 are not below 3, so they are left out.
Example 3
employees = [[2, -5], [4, 3, 2]]id = 2return = -5Nobody reports to employee 2, so the answer is their own importance, -5. Employee 4 is their manager and is not counted.
Constraints
1 <= employees.length <= 4002 <= employees[i].length1 <= employees[i][0] <= 2000, and no two rows have the same employee id.-100 <= employees[i][1] <= 100- Every report id
employees[i][j]withj >= 2is the id of another row, and it appears at most once in that row. - Every employee has at most one direct manager, and no one is their own manager through any chain.
idis the id of one of the rows.
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- Org Chart Report CountsPHONE SCREEN · Seen Dec 2025
- Format the Missing Numbers from 1 to 99PHONE SCREEN · Seen Sep 2025
- Pythagorean Triplets in an ArrayPHONE SCREEN · Seen Oct 2024
- LRU Cache with TTLPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Password GeneratorPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
- Merge IntervalsPHONE SCREEN · ONSITE INTERVIEW · Seen Feb 2022
- JSON ParserPHONE SCREEN · Seen Jun 2021
Similar questions at other companies
- Transform and Prune a Mode-Valued Binary TreeGoogle · PHONE SCREENGoogle interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions
- Top View of a Binary TreeInMobi · ONSITE INTERVIEWInMobi interview questions
- N-ary Index-Path Encoder and DecoderBloomberg LP · PHONE SCREEN · ONSITE INTERVIEWBloomberg LP interview questions
- Account Roles With Parent-User InheritanceStripe · PHONE SCREENStripe interview questions
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Tree Height After Child PromotionSnowflake · PHONE SCREENSnowflake interview questions
- Binary Tree Spiral Level OrderAmazon · PHONE SCREEN · ONSITE INTERVIEW
- Check a Complete Binary TreeGoldman Sachs · PHONE SCREENGoldman Sachs interview questions