DSA interview question
A UI framework lays out nested components as a tree, and deeply nested layouts render slowly. The profiler reports the maximum depth: the number of nodes on the longest path from the root down to a leaf. The recursive answer is one line. An empty tree has depth 0, and otherwise the depth is 1 plus the deeper of the two subtrees. A BFS that counts levels gives the same number without recursion.
Example: [3, 9, 20, null, null, 15, 7] → 3.
int maxDepth(TreeNode* root);
| Input (level order) | Output | Explanation |
|---|---|---|
[3, 9, 20, null, null, 15, 7] | 3 | 3 → 20 → 15 |
[1, null, 2] | 2 | |
[1, 2, null, 3, null, 4] | 4 | A left chain |
[1] | 1 | |
[] | 0 |
(Confirm: depth counts nodes, not edges; an empty tree has depth 0.)
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