Employee Hierarchy Cycle and Depth Analysis
Updated
Problem statement
An organization has n employees labeled from 0 to n - 1. The array manager describes the direct manager relation:
manager[i] = -1means employeeiis the root of the organization.- Otherwise,
manager[i]is the employee who directly manages employeei.
Exactly one employee has manager -1. Every other entry is a valid employee index. The relations may still contain a directed cycle disconnected from the root.
Return a two-element array:
- If any management cycle exists, return
[1, -1]. - Otherwise, return
[0, maxDepth], where an employee's depth is the number of manager edges from the root to that employee andmaxDepthis the greatest employee depth.
Function
analyzeHierarchy(manager: int[]) → int[]Examples
Example 1
manager = [-1,0,0,1,1,3]return = [0,3]No cycle exists. Employee 5 is reached along 0 -> 1 -> 3 -> 5, so the maximum depth is 3.
Example 2
manager = [1,2,0,-1]return = [1,-1]Employees 0, 1, and 2 form the management cycle 0 -> 1 -> 2 -> 0.
Example 3
manager = [-1]return = [0,0]The only employee is the root, whose depth is 0.
Constraints
1 <= manager.length <= 2 * 10^5- Exactly one entry of
manageris-1. - Every other entry is an employee index in
[0, manager.length - 1].
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
- Infer a Lexicographically Smallest Alien AlphabetPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- All Halfway Courses Across Course TracksCrunchyroll · PHONE SCREENCrunchyroll interview questions
- Process Graph Rule ValidationHadrian · PHONE SCREENHadrian interview questions
- Best Currency Conversion PathSquarepoint Capital · PHONE SCREENSquarepoint Capital interview questions
- Longest Matrix Walk with Two-Step ComparisonsGoogle · ONSITE INTERVIEWGoogle interview questions
- Rocket Component CostSpaceX · ONSITE INTERVIEWSpaceX interview questions
- Maximum Reservoir PowerWells Fargo · OAWells Fargo interview questions
- Distance Sums to Special Nodes in a Weighted TreeNutanix · ONSITE INTERVIEWNutanix interview questions
- Shipping Starts and Terminal DestinationsIntuit · OAIntuit interview questions
- Minimum Leaf Path Sum over All RootsInfosys · OAInfosys interview questions
- Longest Decreasing Matrix Path with Limited RelaxationsDuolingo · ONSITE INTERVIEWDuolingo interview questions
Microsoft interview experiences
- Microsoft OA Questions, October 2026: 4 Experiences
- Microsoft Experienced SWE Phone Screen: 1 Experience
- Microsoft Explore and Student SWE: 12 Experiences (Part 6 of 6)
- Microsoft SWE Intern, Early 2025: 41 Experiences (Part 5 of 6)
- Microsoft SWE Intern, Mid 2025: 42 Experiences (Part 4 of 6)
- Microsoft SWE Intern, Late 2025: 47 Experiences (Part 3 of 6)