Insert an Employee into an Organization Hierarchy
Updated
Problem statement
An organization is a rooted tree encoded by strings employeeId|managerId. Exactly one employee is the root and uses manager ID NONE. The order of rows defines the order among siblings.
An employee's score is 1 + number of direct reports. Insert newEmployee into the subtree rooted at requestedManager:
- If the requested manager has fewer than
kdirect reports, attach the new employee there. - Otherwise, search the requested manager's descendants in breadth-first order. Within a level, visit direct reports in their row order.
- Attach the new employee to the first visited employee with fewer than
kdirect reports.
Return the complete updated organization as strings employeeId|managerId|score, sorted by employee ID in ascending lexicographic order.
Function
insertEmployee(organization: String[], requestedManager: String, newEmployee: String, k: int) → String[]Examples
Example 1
organization = ["ceo|NONE","a|ceo","b|ceo","c|a"]requestedManager = "ceo"newEmployee = "d"k = 2return = ["a|ceo|3","b|ceo|1","c|a|1","ceo|NONE|3","d|a|1"]ceo already has two direct reports. Breadth-first search visits a before b; a has one open position, so d is attached there. The scores of a and the new employee are 3 and 1.
Example 2
organization = ["root|NONE","east|root","leaf|east"]requestedManager = "root"newEmployee = "rover"k = 1return = ["east|root|2","leaf|east|2","root|NONE|2","rover|leaf|1"]Both root and east are full. The first available employee in breadth-first order is leaf, so it becomes the new employee's manager.
Constraints
1 <= organization.length <= 200000.1 <= k <= 100000.- Every employee ID is unique, nonempty, case-sensitive, and contains only English letters, digits, underscores, or hyphens.
newEmployeeis not already present, and no employee ID equalsNONE.requestedManageridentifies an existing employee.- The input describes one valid rooted tree, and every employee initially has at most
kdirect reports.
More Waymo Phone Screen questions
- Shortest Travel Time in a Grid with Traffic LightsPHONE SCREEN · Seen Nov 2026
- Fixed-Order Task Run Time With CooldownPHONE SCREEN · Seen Oct 2026
- Reorderable Task Scheduler With CooldownPHONE SCREEN · Seen Oct 2026
- Island Area and Water BoundaryPHONE SCREEN · Seen Sep 2026
- Vehicle Maintenance Event ReadinessPHONE SCREEN · Seen Sep 2026
- Obstacle-Aware Return to OriginPHONE SCREEN · Seen Sep 2026
- Shortest Paths to Multiple TargetsPHONE SCREEN · Seen Sep 2026
- Deterministic Depth-First Graph TraversalPHONE SCREEN · Seen Sep 2026
- Shortest Distance from All BuildingsPHONE SCREEN · Seen Sep 2026
- Fixed-Range Frequency CountPHONE SCREEN · Seen Aug 2026
Similar questions at other companies
- Linked Merchants Within Two HopsStripe · PHONE SCREENStripe interview questions
- Deep Merge with a Conflict ResolverAutodesk · ONSITE INTERVIEWAutodesk interview questions
- Rank Word-Translation Hints from Sentence TranslationsDuolingo · PHONE SCREEN · ONSITE INTERVIEWDuolingo interview questions
- Query Log MatchDatadog · PHONE SCREEN · ONSITE INTERVIEWDatadog interview questions
- In-Memory Filesystem CommandsPerplexity AI · PHONE SCREEN · ONSITE INTERVIEWPerplexity AI interview questions
- Group AnagramsWorkday · PHONE SCREEN
- Find All Paths for a Name in a File SystemThe Walt Disney Company · PHONE SCREENThe Walt Disney Company interview questions
- Group AnagramsEPAM · PHONE SCREENEPAM interview questions
- Flat Records to a Hierarchical TreeAddepar · PHONE SCREEN
- Diff Two Filesystem Hash TreesCursor · PHONE SCREENCursor interview questions