ORIGINAL CODING QUESTION
Widest seating row in a binary chart
Imagine drawing a binary chart on a seating grid: the root takes slot 0 of its row, and a node in slot p places its left child in slot 2p and its right child in slot 2p+1 of the next row. The span of a row is (rightmost occupied slot - leftmost occupied slot + 1), so empty slots between occupied ones still count. Node values do not matter. Return the largest span over all rows.
Original exercise · medium. Editorial role relevance does not imply a company has asked this question.
Input and output
n, then n triples (value,leftChild,rightChild). Node 0 is the root; a missing child is written as -1. Values are present but ignored.
Constraints
1 ≤ n ≤ 200; -100 ≤ value ≤ 100; the triples form one connected binary tree and every child ID is larger than its parent ID. The deepest node is at most 20 edges below the root.
Example 1
Input
7 0 1 2 0 3 4 0 5 6 0 -1 -1 0 -1 -1 0 -1 -1 0 -1 -1
Output
4
The third row fills slots 0 to 3, a span of 4.
Example 2
Input
7 0 1 2 0 3 -1 0 -1 4 0 5 -1 0 -1 6 0 -1 -1 0 -1 -1
Output
8
The fourth row uses only slots 0 and 7, so its span is 8.
Explain your approach
Explain your time and auxiliary space complexity. Which input makes your algorithm work hardest?
Private grading inputs, expected outputs and canonical reference implementations are withheld. Authored expected outputs were cross-checked with independent reference oracles; compiler/runtime behavior still needs a verified execution provider.
Hint 1
Counting nodes per row is not enough; gaps between them count.
Hint 2
Give each node a slot number derived from its parent.
Hint 3
For each row keep the smallest and largest slot seen.
Practice this question
Save your work privately, explain decisions and return to your revision queue.
Open practice workspaceBrowse the question library →