DSA interview question
An org-chart viewer displays a company tree level by level: the CEO, then all direct reports, then their reports, and so on, each level as its own row. For a binary tree, that's a breadth-first search with a queue, where each iteration of the outer loop processes exactly the nodes currently in the queue, which are exactly one level.
Example: [3, 9, 20, null, null, 15, 7] → [[3], [9, 20], [15, 7]].
vector<vector<int>> levelOrder(TreeNode* root);
| Input (level order) | Output | Explanation |
|---|---|---|
[3, 9, 20, null, null, 15, 7] | [[3], [9, 20], [15, 7]] | |
[1] | [[1]] | |
[] | [] | |
[1, 2, 3, 4, null, null, 5] | [[1], [2, 3], [4, 5]] | Gaps don't matter |
[1, 2, null, 3] | [[1], [2], [3]] | A left chain |
(Confirm: each inner list lists one level from left to right.)
On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in