← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

LinkedIn software engineer interview with a tree problem that felt straightforward once I remembered to actually use the BST property instead of treating it like a generic binary tree.

Questions Asked (1)

Q1

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

Algorithms & Data Structures
Author's notes

My first instinct was to do some kind of path-tracking approach, storing ancestors in a set, which works but completely ignores the BST structure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Leverage the BST property: starting from the root, if both p and q are less than the current node, move left; if both are greater, move right; otherwise, the current node is the lowest common ancestor. This iterative approach runs in O(h) time and O(1) space, where h is the tree height.

Pro tip: Clarify that this solution exploits the BST ordering, unlike the general binary tree LCA which requires post-order traversal. Mention that the iterative approach avoids recursion stack overhead and is more space-efficient.

1. Clarify assumptions and edge cases

Confirm that the tree is a BST, nodes p and q are distinct and guaranteed to exist, and that node values are unique. Discuss edge cases like p or q being the root, or one being an ancestor of the other.

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 ordering allows us to decide the direction to traverse.

3. Describe the iterative traversal

Start at the root. While the current node is not null, compare its value with p and q. If both are smaller, go left; if both are larger, go right; otherwise, return the current node as the LCA.

4. Analyze complexity

Time complexity is O(h) where h is the height of the tree (O(log n) for balanced BST, O(n) worst-case). Space complexity is O(1) for iterative, O(h) for recursive due to call stack.

5. Discuss alternative approaches

Mention that a recursive solution is also possible but uses stack space. Contrast with the general binary tree LCA algorithm which requires post-order traversal and does not assume BST ordering.

Key Points to Mention

  • BST property: left subtree values < node < right subtree values
  • Iterative traversal avoids recursion stack and is O(1) space
  • Time complexity O(h), where h is tree height
  • Handling cases where p or q is an ancestor of the other
  • Comparison with general binary tree LCA (no BST assumption)
  • Edge cases: p or q equals root, skewed tree

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