Meta·Software Engineer·Onsite - Coding / Algorithms
- Given the root of a binary tree and two nodes p and q (both guaranteed to exist), find their lowest common ancestor. The LCA is the deepest node that is an ancestor of both.
- If the tree is a BST, how would you use the BST property to find the LCA more efficiently?
- If each node has a pointer to its parent, how would you compute the LCA more efficiently?
- If the binary tree is too large to fit in memory, how would you design a system to compute LCA, considering I/O constraints, indexing, blocking, and both preprocessing and online query scenarios?
“Got through this fine with the standard recursive post-order approach.”