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
Coding & Algorithms
Software Engineer
TikTok
November 29, 2026
Software Engineer
Phone Screen
Coding & Algorithms
Easy

0

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
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 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 Problems
Sample Answer
Requirements
  • Node has name, left, right.
  • Inputs: root, names a, b.
  • Output: the LCA node's name, or None if 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. ...


Submit Your Answer
Markdown supported