DSA interview question
A photographer shoots a tree sculpture from the right side and then from the left side. From the right, each level shows its rightmost node; from the left, its leftmost. A DFS that tracks depth captures either view in one pass. For the right view, visit the right child before the left; the first node reached at each new depth is the visible one. For the left view, visit left first.
Example: [1, 2, 3, null, 5, null, 4] → right view [1, 3, 4], left view [1, 2, 5].
vector<int> rightSideView(TreeNode* root);
vector<int> leftSideView(TreeNode* root);
| Input (level order) | Output | Explanation |
|---|---|---|
[1, 2, 3, null, 5, null, 4] | right [1, 3, 4] left [1, 2, 5] | |
[1, 2, 3, 4] | right [1, 3, 4] left [1, 2, 4] | 4 shows from both sides |
[1, null, 3] | right [1, 3] left [1, 3] | |
[1, 2] | right [1, 2] left [1, 2] | |
[] | right [] left [] |
(Confirm: one node per level, top to bottom; a node hidden behind others on its level isn't visible.)
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