Org Chart Report Counts
Updated
Problem statement
A company's org chart is written as a nested dictionary. Its top-level keys are the people at the top of the company. Each person maps to a dictionary of the people who report directly to them, in the same format, or to null when no one reports to them.
In this problem the chart is passed as orgChart, with one row per person. A row starts with a person's name and then lists that person's direct reports. A person with no direct reports has a row that holds only their name. The rows can come in any order.
A person with at least one direct report is a manager. A person with no direct reports is an individual contributor, or IC. A person's total reports are everyone below them in the chart: their direct reports, the direct reports of those people, and so on down.
Given the name empName, return "IC,0,0" if that person is an IC. If the person is a manager, return "manager,d,t", where d is their number of direct reports and t is their number of total reports.
Follow-up: if the chart stays the same and you are asked about many people, would you count again on every call or work out all the counts once? And if a new IC can be added under any existing person, how do you keep the direct and total counts right for everyone above the new IC?
Function
lookup(orgChart: List<List<String>>, empName: String) → StringExamples
Example 1
orgChart = [["a","a1","a2"],["a1","a11","a12"],["a11"],["a12"],["a2"],["b"]]empName = "a"return = "manager,2,4"As a nested dictionary, this chart is {"a": {"a1": {"a11": null, "a12": null}, "a2": null}, "b": null}. The people a1 and a2 report directly to a, and a11 and a12 report to a1. So a has 2 direct reports and 4 total reports.
Example 2
orgChart = [["Person1","Person2","Person3"],["Person2"],["Person3","Person4"],["Person4","Person5"],["Person5"]]empName = "Person3"return = "manager,1,2"Only Person4 reports directly to Person3, and Person5 reports to Person4. That gives Person3 1 direct report and 2 total reports.
Example 3
orgChart = [["a","a1","a2"],["a1","a11","a12"],["a11"],["a12"],["a2"],["b"]]empName = "b"return = "IC,0,0"The person b is at the top of the chart, but no one reports to b, so b is an IC.
Constraints
1 <= orgChart.length <= 5001 <= orgChart[i].length <= 5001 <= orgChart[i][j].length <= 20- Every name is made of English letters and digits.
- Every person has exactly one row: the first names of the rows are all different, and every name in
orgChartis the first name of some row. - Each person is listed as a direct report in at most one row, and no person is below themselves in the chart.
empNameis the first name of one of the rows.
More Patreon Phone Screen questions
- Sliding-Window API Rate LimiterPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Employee Importance from a DictionaryPHONE SCREEN · Seen Feb 2026
- Uniform Random Number from Coin TossesPHONE SCREEN · Seen Feb 2026
- Wordle Guess CheckerPHONE SCREEN · Seen Jan 2026
- 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
- Mode of a Ternary Search TreeGoogle · PHONE SCREENGoogle 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
- Validate a BST with a Custom ComparisonApple · PHONE SCREENApple interview questions
- Count Distinct Rooted-Tree GraftsThe D.E. Shaw Group · PHONE SCREENThe D.E. Shaw Group interview questions
- Lowest Common Manager for Employee GroupsAmazon · PHONE SCREEN
- Merge N-Ary Trees by KeyLinkedIn · PHONE SCREEN · ONSITE INTERVIEWLinkedIn interview questions
- Filesystem Hash TreeCursor · PHONE SCREEN · ONSITE INTERVIEWCursor interview questions
- Render a Stable Comment HierarchyBobyard · PHONE SCREEN
- Translate Values in a Nested MapZip · PHONE SCREENZip interview questions