← Microsoft Interview Insights

Microsoft·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
May 2026

Summary

Microsoft SWE interview, got a tree problem that seemed straightforward until I actually had to think about it under pressure.

Questions Asked (1)

Q1

Given a binary search tree and two nodes p and q, find their lowest common ancestor.

Algorithms & Data Structures
Author's notes

My first instinct was to treat it like a generic tree and do some kind of recursive scan, which would've worked but completely missed the point.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and assumptions, then explain the BST property that allows an efficient solution. Walk through the iterative approach that traverses from the root, moving left or right based on the values of p and q, and finally return the node where they split.

Pro tip: Mention that the BST property enables O(h) time and O(1) space, which is optimal. Also, briefly discuss how the solution changes if the tree is not a BST, showing depth of understanding.

1. Clarify the problem

Confirm that the tree is a BST, nodes p and q are guaranteed to exist, and we need the lowest common ancestor (LCA). Ask if the tree can be modified or if we can use extra space.

2. Explain the BST property

State that for any node, all values in the left subtree are smaller and all values in the right subtree are larger. This allows us to decide the direction of traversal based on the values of p and q.

3. Describe the iterative algorithm

Start from the root. While the current node is not null, if both p and q are smaller than the current node, move to the left child; if both are larger, move to the right child; otherwise, the current node is the LCA.

4. Analyze complexity

Time complexity is O(h) where h is the height of the tree, and space complexity is O(1) for the iterative approach. Mention that recursion would use O(h) space.

5. Handle edge cases and alternatives

Discuss cases where p or q is the root, or one is an ancestor of the other. Also, briefly mention how to solve it if the tree were not a BST (e.g., using recursion or parent pointers).

Key Points to Mention

  • BST property: left subtree values < node value < right subtree values
  • Iterative traversal from root, moving left or right based on comparisons
  • LCA is the first node where p and q are on different sides (or one equals the node)
  • Time complexity O(h), space complexity O(1) for iterative
  • Edge cases: p or q is root, one is ancestor of the other
  • Alternative for non-BST: recursive post-order traversal or parent pointers

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.