Level Order
Python data engineering interview problem. Difficulty: advanced. Pattern: Trees. About 18 minutes. Part of the Pro drill bank.
Return level-order traversal as a list of levels. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A tree report needs rows grouped by depth so each level can be displayed or processed separately. Return a list of lists. The first inner list contains the root, the next contains its children, and so on from left to right. Write level_order(level_order_input: list) -> list. The sample tree produces [[3], [9, 20], [15, 7]]. The starter file already has the function name and harness print lines. Keep them and fill only the function body.
Requirements
- Visit nodes breadth first.
- Keep left-to-right order inside each level.
- Return one inner list per visited level.
Constraints
- The input is level-order with None placeholders.
- The return type is a list of lists.
Examples
Input: level_order([3, 9, 20, None, None, 15, 7]) Output: [[3], [9, 20], [15, 7]] 3 levels.
Topics: lakebench, python, bfs, levels.
More Python interview questions · All interview problems · Learn data engineering
Level Order
Interview-style drill: Return level-order traversal as a list of levels.
Implement `level_order(level_order_input: list) -> list` returning `[[level0], [level1], ...]`. Example: `[3, 9, 20, None, None, 15, 7]` → `[[3], [9, 20], [15, 7]]`. Keep the harness.