Skip to main content

77. Lowest Common Ancestor of a Binary Search Tree

mediumAsked at Workday

Find the lowest common ancestor of two nodes in a BST. Workday uses this for org-chart-style 'common manager' queries — direct domain fit.

By Alex Chen, Founder, InterviewChamp.AI · Last verified

Source citations

Public interview reports confirming this problem appears in Workday loops.

  • Glassdoor (2026-Q1)Workday org-chart team — direct analogy.
  • Blind (2025)Workday Pleasanton phone screen.

Problem

Given a binary search tree (BST), find the lowest common ancestor (LCA) node of two given nodes in the BST.

Constraints

  • The number of nodes in the tree is in the range [2, 10^5].
  • -10^9 <= Node.val <= 10^9
  • All Node.val are unique.
  • p != q.
  • p and q will exist in the BST.

Examples

Example 1

Input
root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 8
Output
6

Example 2

Input
root = [6,2,8,0,4,7,9,null,null,3,5], p = 2, q = 4
Output
2

Approaches

1. Generic LCA (ignore BST property)

DFS as in LC 236.

Time
O(n)
Space
O(h)
// works but ignores the BST property

Tradeoff: Correct but doesn't exploit BST ordering.

2. Walk down using BST ordering

Both p and q smaller than current -> go left. Both larger -> go right. Else current is LCA.

Time
O(h)
Space
O(1)
function lowestCommonAncestor(root, p, q) {
  let node = root;
  while (node) {
    if (p.val < node.val && q.val < node.val) node = node.left;
    else if (p.val > node.val && q.val > node.val) node = node.right;
    else return node;
  }
  return null;
}

Tradeoff: O(h) iterative. The 'split point' (where p and q go different directions, or one is the current node) is the LCA.

Workday-specific tips

Workday LOVES this question because of the direct org-chart analogy. Use the BST property — exploiting structure earns more credit than the generic LC 236 solution.

Common mistakes

  • Using the generic LCA algorithm — works but misses the chance to exploit BST.
  • Using <= or >= — fails when one of p, q is the LCA itself.
  • Recursing when iteration is shorter.

Follow-up questions

An interviewer at Workday may pivot to one of these next:

  • LCA of Binary Tree (LC 236) — without BST property.
  • LCA with parent pointers.
  • LCA of multiple nodes.

Solve it now

Free. No sign-up. Python and JavaScript run instantly in your browser.

Output

Press Run or Cmd+Enter to execute

FAQ

Why split point?

In a BST, p and q are in p < q ordering by value. The LCA is the first node where one is to the left and the other to the right (or one equals the current node).

Iterative or recursive?

Both work. Iterative is O(1) space; recursive uses O(h) stack. Iterative is the cleaner answer.

Practice these live with InterviewChamp.AI

Drill Lowest Common Ancestor of a Binary Search Tree and other Workday interview questions under real-loop conditions with instant feedback on your reasoning, complexity claims, and code.

Practice these live with InterviewChamp.AI →