Interview Questions/Coding/Largest Division Signal Load

Largest Division Signal Load

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

Hard

Largest Division Signal Load

An organization chart is described by parent pointers. Each node has a load value, which may be positive or negative. The load of a division rooted at a node is the sum of load values for that node and every employee below it in the chart. Your task is to find the heaviest division anywhere in the organization, not necessarily the root division. Input Format: You are given arrays parent and load. parent[i] is the manager of node i, and -1 marks the root. Output Format: Return the maximum division load over all rooted subtrees.

Examples

Example 1
Input:
parent = [-1, 0, 0, 1, 1]
load = [5, -2, 3, 4, -1]
Output:
9

Explanation: Subtree sums by node are [9, 1, 3, 4, -1]. The largest is node 0 with load 9, so the output is 9.

Approach hint

Build children from the parent array.

Common mistake

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

solution.cpp