DSA interview question
Now look at the tree from below. For each horizontal distance (root 0, left −1, right +1), the visible node is the lowest one. If two nodes share the lowest level at the same distance, the one appearing later in level order wins. A BFS that keeps overwriting the entry for each distance ends with exactly that node, because BFS visits deeper levels later and, within a level, from left to right.
Example: [20, 8, 22, 5, 3, null, 25, null, null, 10, 14] → [5, 10, 3, 14, 25].
vector<int> bottomView(TreeNode* root);
| Input (level order) | Output | Explanation |
|---|---|---|
[20, 8, 22, 5, 3, null, 25, null, null, 10, 14] | [5, 10, 3, 14, 25] | 10 and 14 hide 8 and 22 |
[20, 8, 22, 5, 3, 4, 25, null, null, 10, 14] | [5, 10, 4, 14, 25] | 3 and 4 tie at distance 0; the later one (4) wins |
[1, 2, 3] | [2, 1, 3] | |
[1] | [1] | |
[] | [] |
(Confirm: the lowest node per horizontal distance; ties on the same level go to the one later in level order.)
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