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
Coding & Algorithms
Software Engineer
Flipkart
November 29, 2026
Software Engineer
Technical Screen
Coding & Algorithms
Medium

0

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
binary trees
lowest common ancestor
recursion
post-order traversal
How to Approach This
  1. Clarify input constraints and edge cases before writing code.
  2. Walk through your approach verbally and confirm with the interviewer before coding.
  3. Start with a brute force solution, then optimize. Mention time and space complexity.
  4. Test your solution with examples, including edge cases like empty input or duplicates.
  5. 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 Problems
Sample Answer
Requirements
  • Input: root of a binary tree with unique category names, two names a and b.
  • 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...


Submit Your Answer
Markdown supported

Related Questions