Find the maximum depth of a binary tree
Reported in Rivian USA engineering loops. Tree recursion warm-up that often leads to balanced-tree and diameter follow-ups.
Interview scenario
Often asked in Rivian on-site or virtual loops at US offices (Bay Area, Seattle, NYC, Austin, and remote US). Prepare a clear spoken answer plus key trade-offs.
Model answer
Try answering aloud first
Cover trade-offs, structure, and a concrete example before revealing the baseline response.
How to frame this at Rivian: Connect your answer to measurable impact, clarity of thought, and trade-offs the team cares about. Below is a strong baseline response you can adapt with your own project examples.
Max depth is 1 + max(depth(left), depth(right)) with base case null → 0. This is a post-order computation: you need child depths before combining at the parent.
function maxDepth(root) {
if (!root) return 0;
return 1 + Math.max(maxDepth(root.left), maxDepth(root.right));
}Complexity: O(n) time visiting each node once; O(h) recursion stack where h is height (O(n) worst case for skewed tree).
Iterative BFS counts levels by processing queue size per level—useful when stack depth is a concern. Follow-ups: minimum depth, check if balanced (compare left/right heights at each node), tree diameter, and serialize/deserialize.
Discussion
Comments (0)
Share how this question came up in your loop, or add tips for others preparing.
Log in to comment on this question.