Lowest Shared Category of Two Products in a Taxonomy Tree
Last updated: November 29, 2026
Quick Overview
Flipkart's catalog taxonomy is a binary tree where each node is a category and leaves are product types. Given the root and two node values, return the deepest category that contains both.
Flipkart
November 29, 20260
0
0 solved
Flipkart's catalog taxonomy is a binary tree where each node is a category and leaves are product types. Given the root and two node values, return the deepest category that contains both.
Asked in Flipkart technical screens with a catalog spin: deciding the broadest shared category for cross sell rules or for breadcrumb display when two items are compared. The interviewer wants the single pass recursive LCA and a clear explanation of why it works.
What the Interviewer Expects
- Clarify whether both values are guaranteed to exist in the tree
- Write the recursive post order LCA that returns the node found in each subtree
- Explain the case where one target is an ancestor of the other
- State O(n) time and O(h) stack space
- Discuss an iterative or parent pointer variant for deep 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 handle the case where one of the values may be missing?
- If the taxonomy is a general n-ary tree with parent pointers, what changes?
- How would you answer many LCA queries quickly on a static taxonomy?
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: root of a binary tree with unique category names, two names
aandb. - Assume both exist (confirm; handle the other case as a follow up).
- Output: the deepest node that has both in its s...
High-Level Design
Do one post order traversal. Each call returns: the node itself if it is a or b; otherwise whatever its children found. If both children return something non null, the current node is where the tw...