Find the Maximum Depth of a Product Category Tree
Last updated: November 29, 2026
Quick Overview
Amazon's browse tree is an n-ary tree of categories where each node has a name and a list of child categories. Return the depth of the deepest category, implement it both recursively and iteratively, and explain which you would ship for a tree that could be thousands of levels deep.
Amazon
November 29, 20260
0
0 solved
Amazon's browse tree is an n-ary tree of categories where each node has a name and a list of child categories. Return the depth of the deepest category, implement it both recursively and iteratively, and explain which you would ship for a tree that could be thousands of levels deep.
A gentle tree question used in Amazon phone screens, set in the catalog browse hierarchy. The interviewer is checking basic recursion, the ability to convert it to an explicit stack or queue, and awareness of recursion limits on deep or malformed data.
What the Interviewer Expects
- Define depth precisely (root alone is depth 1, empty tree is 0)
- Give the recursive solution
- Give an iterative BFS by levels
- Discuss Python recursion limit and stack overflow risk
- Mention cycle protection if data is not guaranteed to be a tree
Key Topics to Cover
How to Approach This
- Clarify input constraints and edge cases before writing code.
- Walk through your approach verbally and confirm with the interviewer before coding.
- Start with a brute force solution, then optimize. Mention time and space complexity.
- Test your solution with examples, including edge cases like empty input or duplicates.
- Consider common patterns: sliding window, two pointers, hash map, BFS/DFS, dynamic programming.
Possible Follow-up Questions
- How would you return the path to the deepest category, not just its depth?
- What if the hierarchy is stored as parent pointers in a table instead?
- How would you detect a cycle introduced by a bad catalog edit?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Each node has name and children (list). Input may be None. Depth of a single node is 1. Return an int....
High-Level Design
Recursive: depth(node) = 1 + max(depth(child)), with 0 for a leaf's missing children. Very readable but uses call stack proportional to depth.
Iterative: BFS level by level with a deque. Each time we...