EvalFrame Technical Mock Interviews & Coding Practice

Lowest Common Ancestor of a BST

Easy · Binary Search Tree · Algorithm problem · all problems

Given a binary search tree (BST) and two values `p` and `q`, find the lowest common ancestor (LCA) node value. According to the definition of LCA on Wikipedia: "The lowest common ancestor is defined between two nodes p and q as the lowest node in T that has both p and q as descendants (where we allow a node to be a descendant of itself)." The tree is given as a level-order array of numbers. **Examples:** ```typescript lowestCommonAncestor([6,2,8,0,4,7,9,null,null,3,5], 2, 8) // 6 lowestCommonAncestor([6,2,8,0,4,7,9,null,null,3,5], 2, 4) // 2 ```

Solve It in the BrowserPractice Explaining It Out Loud

Optimal Approach

  • Time complexity: O(h)
  • Space complexity: O(1)

Traverses down a single path of the BST from root to leaf, taking O(h) time where h is the tree height.

Follow-Up Questions Interviewers Ask About Lowest Common Ancestor of a BST

  • How does BST property simplify LCA compared to a general binary tree?
    In a BST, we can simply compare node values: if both p and q are smaller, go left; if both are larger, go right. The split point is the LCA.

Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.