Closest Shared Category for Two Videos in a Taxonomy Tree
Last updated: November 29, 2026
Quick Overview
TikTok's content taxonomy is stored as a binary tree of category nodes with unique names, and every video is tagged with one node. Given the root and two category names, return the deepest category that is an ancestor of both (a node counts as its own ancestor).
TikTok
November 29, 20260
0
0 solved
TikTok's content taxonomy is stored as a binary tree of category nodes with unique names, and every video is tagged with one node. Given the root and two category names, return the deepest category that is an ancestor of both (a node counts as its own ancestor).
Content understanding teams use category hierarchies to compute how related two videos are, for example for diversity rules in the For You feed. This phone screen question checks the standard lowest common ancestor recursion and how you handle missing nodes.
What the Interviewer Expects
- Uses a single post-order traversal returning a found node or None
- Returns the current node when targets are found in both subtrees
- Treats a node as its own ancestor
- Mentions the case where one name is missing from the tree
- Gives O(n) time and O(h) space
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 taxonomy?
- If the taxonomy were a DAG (a category with two parents), what changes?
- How would you turn LCA depth into a similarity score?
Sharpen Your Skills on Codemia
Practice similar problems with our interactive workspace, get AI feedback, and track your progress.
Practice DSA ProblemsSample Answer
Requirements
- Node has
name,left,right. - Inputs:
root, namesa,b. - Output: the LCA node's name, or
Noneif either name is absent....
High-Level Design
Post-order search: each call returns the node if it matches a target, otherwise whatever its children return. If both children return something, the current node is the split point, which is the LCA. ...