Lowest Common Category for Two Products in a Taxonomy Tree
Last updated: November 29, 2026
Quick Overview
Walmart's product taxonomy is stored as a binary tree of category names. Given two category names, return the deepest category that contains both, or None if either name is not in the tree.
Walmart Labs
November 29, 20260
0
0 solved
Walmart's product taxonomy is stored as a binary tree of category names. Given two category names, return the deepest category that contains both, or None if either name is not in the tree.
Catalog teams at Walmart Labs use taxonomy trees to decide where a merchandising rule or a shelf placement applies to two related items. The interviewer wants a single post-order pass and checks the subtle case where one name is missing, which the textbook LCA gets wrong.
What the Interviewer Expects
- Use one post-order traversal
- Return the first node whose subtree holds both targets
- Verify both targets exist instead of assuming it
- Handle one target being an ancestor of the other
- Discuss recursion depth for skewed trees
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 answer many LCA queries quickly on a fixed tree?
- What if the taxonomy is an n-ary tree with parent pointers?
- How would you handle a product that sits under two categories, making it a DAG?
- How would you do this iteratively?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
Input: the root of a binary tree where each node has a unique name, plus two names a and b. Output: the name of the deepest node whose subtree contains both, where a node counts as its own descendan...
High-Level Design
Do a post-order walk that returns how many targets the subtree contains. A node's count is the sum of its children plus one if the node itself is a target. The first node, in post-order, whose count r...