Count Levels
Updated
Problem statement
A company wants to track change in their organization by knowing how many levels exist between any two employees. This number will help them know who is being promoted and who is not.
For example: If Susan reports to John and John reports to Amy. Then, there are 2 levels between Susan and Amy.
Write a program that will count how many levels exist between any two names in a hierarchy of employees. The program must read a list of name pairs that represent an employee and their manager.
HINT: The two names to compare may be in different parts of the organizational tree and not have a direct managerial line.
Input: The first line of input will be a pair of names to compare.
All subsequent lines will be employee/manager pairs. The company's complete hierarchy will be included so no incomplete trees will exist.
Function
countLevels(employeePairs: String[][], employee1: String, employee2: String) → intExamples
Example 1
employeePairs = [["Susan", "John"], ["John", "Amy"]]employee1 = "Susan"employee2 = "Amy"return = 2If Susan reports to John and John reports to Amy. Then, there are 2 levels between Susan and Amy.
Example 2
employeePairs = [["Terry", "David"], ["Kyle", "David"], ["Ben", "Kyle"], ["Scott", "Jon"], ["Chris", "Scott"], ["Jon", "Kenny"], ["Kenny", "David"], ["David", "Mike"]]employee1 = "Scott"employee2 = "David"return = 3To find the levels between Scott and David, we can trace the hierarchy as follows: Scott reports to Jon, Jon reports to Kenny, and Kenny reports to David. Therefore, there are 3 levels between Scott and David.
More BlackRock interview questions
- Find Minimum TimeOA · Seen Nov 2026
- Valid ParenthesesOA · Seen Sep 2026
- Lowest Common Ancestor in a Parent TreeOA · Seen Sep 2026
- Road Trip (Analyst Program :POA · Seen Aug 2026
- Set IntersectionOA · Seen Aug 2026
- Process SchedulingPHONE SCREEN · Seen Mar 2026
- John Venn (Analyst Program :)OA · Seen Sep 2024
- Corporate LadderOA · Seen Jul 2024
- Happy NumberOA · Seen Jul 2024
Similar questions at other companies
- Delete Nodes and Return Forest RootsGoogle · ONSITE INTERVIEWGoogle interview questions
- Group Identical Tree LevelsSalesforce · ONSITE INTERVIEWSalesforce interview questions
- Insert an Employee into an Organization HierarchyWaymo · PHONE SCREENWaymo interview questions
- Bottom View of a Binary TreeOracle · PHONE SCREEN · ONSITE INTERVIEWOracle interview questions
- Vertical Order Traversal of a Binary TreeAmazon · ONSITE INTERVIEW
- Decrypt a String from Binary Tree ColumnsBloomberg LP · PHONE SCREENBloomberg LP interview questions
- Employee Importance from a DictionaryPatreon · PHONE SCREENPatreon interview questions
- Binary Tree Vertical Order with Stable TiesMeta · ONSITE INTERVIEWMeta interview questions
- Everyone Under a ManagerAdobe · PHONE SCREENAdobe interview questions
- Team Cost Under a ManagerRippling · PHONE SCREENRippling interview questions