Interview Questions/Coding/Hierarchy Banquet Layers

Hierarchy Banquet Layers

Preview mode. Log in to edit, run, submit, and save progress.

Medium

Hierarchy Banquet Layers

A company is assigning banquet seating layers for one or more office hierarchies. Every employee has either no direct manager, marked as -1, or exactly one direct manager. The reporting data is guaranteed to be a forest, so there are no cycles. A top-level employee sits in layer 1, their direct reports sit in layer 2, and so on. For each hierarchy batch, report the maximum number of layers needed. Input Format: The first line contains t, the number of test cases. Each test case contains n, followed by n lines where the i-th line gives employee i's direct manager or -1. Output Format: For each test case, print the maximum depth of any reporting chain.

Examples

Example 1
Input:
1
5
-1
1
2
1
-1
Output:
3

Explanation: Employee 1 manages employee 2, and employee 2 manages employee 3. That chain uses three layers, which is the deepest chain in the batch.

Approach hint

Start with the simplest clear approach, explain the trade-off, then move toward the cleaner answer.

Common mistake

Skipping assumptions, edge cases, or trade-offs can make an otherwise good answer feel incomplete.

solution.cpp