← LinkedIn Interview Insights

LinkedIn·AI Engineer·Technical Phone Screen·Senior

SeniorPrefer not to say
May 2026

Summary

Had a technical phone screen for an AI Engineer role at LinkedIn. One algorithm question, pretty focused, they wanted both the solution and a complexity proof which I wasn't fully expecting.

Questions Asked (1)

Q1

Given the root of a binary search tree and two nodes p and q, find their lowest common ancestor. Also prove the time complexity of your solution.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The traversal logic itself clicked pretty fast.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and constraints, then explain the BST property that allows an efficient solution. Present the iterative approach that traverses from the root, moving left or right based on the values of p and q, and finally prove the O(h) time complexity with a clear argument.

Pro tip: Mention that the iterative solution uses O(1) space, which is better than the recursive approach, and discuss how the algorithm handles edge cases like when one node is an ancestor of the other.

1. Clarify the problem

Confirm that the tree is a BST, that p and q are guaranteed to exist, and that nodes have unique values. Ask if the tree can be modified or if recursion is preferred.

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 property allows us to decide the direction of traversal based on the values of p and q.

3. Present the algorithm

Describe the iterative approach: start at the root, while the current node is not null, if both p and q are smaller, move left; if both are larger, move right; otherwise, the current node is the LCA.

4. Prove time complexity

Argue that in the worst case, we traverse from the root to the deepest leaf, visiting at most h nodes, where h is the height of the tree. Thus, time complexity is O(h), which is O(log n) for a balanced BST and O(n) for a skewed tree.

5. Discuss space complexity and edge cases

Highlight that the iterative solution uses O(1) extra space. Mention edge cases: p or q is the root, p is an ancestor of q, or the tree is skewed.

Key Points to Mention

  • BST property: left subtree values < node value < right subtree values
  • Iterative traversal avoids recursion stack, achieving O(1) space
  • Time complexity O(h) where h is tree height; best case O(log n), worst case O(n)
  • Handling edge cases: when one node is an ancestor of the other, the ancestor is the LCA
  • Comparison with alternative approaches: recursive solution uses O(h) space, path-based approach uses extra space
  • Clarify assumptions: nodes are guaranteed to exist, values are unique

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