Lowest Common Dispatch Region for Two City Nodes
Last updated: November 29, 2026
Quick Overview
Uber's dispatch regions form a binary tree where each node is a region and its children are sub-regions. Given the root and two distinct region nodes p and q that both exist in the tree, return their lowest common ancestor region.
Uber
November 29, 20260
0
0 solved
Uber's dispatch regions form a binary tree where each node is a region and its children are sub-regions. Given the root and two distinct region nodes p and q that both exist in the tree, return their lowest common ancestor region.
A warm-up in Uber phone screens that checks whether you can reason about recursion on trees before the harder second question. Interviewers look for a single-pass solution and a clear statement of what each recursive call returns.
What the Interviewer Expects
- States that the tree is a plain binary tree, not a BST, so values cannot guide the search
- Writes a single post-order recursion that returns the found node or None
- Explains why the first node where both sides return non-null is the answer
- Handles the case where p is an ancestor of q
- Gives O(n) time and O(h) stack 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 on the same static region tree quickly?
- What changes if p or q might be missing from the tree?
- How would you do this with parent pointers and no root access?
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, two node references
pandq, both guaranteed present. - Output: the deepest node that has both as descendants (a node counts as its own descendant).
- Region IDs ar...
High-Level Design
Do one post-order traversal. Each call answers: which of p or q did I see in this subtree? If the current node is p or q, return it. Otherwise recurse left and right. If both sides return something,...