Lowest Common Interest Node for a Set of Taxonomy Topics
Last updated: November 29, 2026
Quick Overview
Pinterest organizes topics in an interest taxonomy tree (for example all, home, kitchen, pantry). Given the tree and a set of topic names a user engaged with, return the most specific topic whose subtree contains all of them, or None if any topic is missing.
0
0
0 solved
Pinterest organizes topics in an interest taxonomy tree (for example all, home, kitchen, pantry). Given the tree and a set of topic names a user engaged with, return the most specific topic whose subtree contains all of them, or None if any topic is missing.
Interest taxonomies drive targeting, ads categories, and topic follow suggestions at Pinterest. The question extends the classic two-node lowest common ancestor to many nodes in an n-ary tree, and the interviewer checks a single post-order pass and correct handling of missing targets and targets that are ancestors of each other.
What the Interviewer Expects
- Uses one post-order DFS that counts targets in each subtree
- Records the first (deepest) node whose count equals the target count
- Handles a target that is an ancestor of another target
- Returns None when a target does not exist instead of a wrong ancestor
- Mentions recursion depth limits and an iterative alternative
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 such queries quickly on a fixed taxonomy?
- What if the taxonomy is a DAG where a topic can have two parents?
- How would you pick a topic that covers most but not all of the user's interests?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Tree is n-ary with unique names. Targets are a set of names.
- Output: name of the deepest node whose subtree contains all targets, or None if any target is absent or the set is empty....
High-Level Design
Post-order DFS returns how many targets lie in each subtree (including the node itself). Children finish before parents, so the first node to reach the full count is the deepest such node. If the root...