PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Coding & Algorithms/Microsoft

Find lowest common ancestor in tree

Last updated: Mar 29, 2026

Quick Overview

This question evaluates understanding of tree data structures and the lowest common ancestor concept, contrasting use of binary search tree ordering with the general binary tree case while requiring algorithmic time and space complexity reasoning in the Coding & Algorithms domain.

  • medium
  • Microsoft
  • Coding & Algorithms
  • Software Engineer

Find lowest common ancestor in tree

Company: Microsoft

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

You are given the root of a binary search tree (BST) and two distinct nodes p and q that are guaranteed to exist in the tree. Task (BST case): - Find the lowest common ancestor (LCA) of nodes p and q. - The LCA is defined as the lowest (deepest) node in the tree that has both p and q as descendants (a node can be a descendant of itself). Assumptions: - The tree satisfies the BST property: for any node x, all nodes in the left subtree have values < x.val, and all nodes in the right subtree have values > x.val. - You may receive p and q as node references or as values; clarify with the interviewer, and handle one consistent version. Follow-up: - How would your approach change if the tree is a general binary tree **without** the BST property? - In the general binary tree case: - The tree may not be balanced. - Node values are not ordered. - p and q are still guaranteed to exist. Describe algorithms for both the BST and the general binary tree cases and analyze their time and space complexity.

Quick Answer: This question evaluates understanding of tree data structures and the lowest common ancestor concept, contrasting use of binary search tree ordering with the general binary tree case while requiring algorithmic time and space complexity reasoning in the Coding & Algorithms domain.

Related Interview Questions

  • Sort Three Categories In Place - Microsoft (medium)
  • Implement K-Means and Detect Divisible Subarrays - Microsoft (medium)
  • Implement SFT Sample Packing - Microsoft (medium)
  • Implement SQL Table and DNA Ordering - Microsoft (medium)
  • Solve power jumps and graph tour - Microsoft (hard)
Microsoft logo
Microsoft
Nov 19, 2025, 12:00 AM
Software Engineer
Technical Screen
Coding & Algorithms
4
0

You are given the root of a binary search tree (BST) and two distinct nodes p and q that are guaranteed to exist in the tree.

Task (BST case):

  • Find the lowest common ancestor (LCA) of nodes p and q.
  • The LCA is defined as the lowest (deepest) node in the tree that has both p and q as descendants (a node can be a descendant of itself).

Assumptions:

  • The tree satisfies the BST property: for any node x, all nodes in the left subtree have values < x.val, and all nodes in the right subtree have values > x.val.
  • You may receive p and q as node references or as values; clarify with the interviewer, and handle one consistent version.

Follow-up:

  • How would your approach change if the tree is a general binary tree without the BST property?
  • In the general binary tree case:
    • The tree may not be balanced.
    • Node values are not ordered.
    • p and q are still guaranteed to exist.

Describe algorithms for both the BST and the general binary tree cases and analyze their time and space complexity.

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Coding & Algorithms•More Microsoft•More Software Engineer•Microsoft Software Engineer•Microsoft Coding & Algorithms•Software Engineer Coding & Algorithms
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.