← LinkedIn Interview Insights

LinkedIn·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jul 2026

Summary

LinkedIn software engineer interview with a classic tree problem and a follow-up that actually made me think harder than the main question.

Questions Asked (1)

Q1

Given a binary tree, find the lowest common ancestor of two given nodes. Follow-up: how does your approach change if every node has a pointer to its parent?

Algorithms & Data Structures
Author's notes

The base recursive solution came back to me pretty quickly.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem: confirm whether nodes are guaranteed to be in the tree, whether a node can be an ancestor of itself, and whether the tree is binary (not necessarily BST). Then present a recursive post-order traversal solution for the general binary tree, and for the follow-up, describe using parent pointers to find the LCA via a two-pointer technique or by marking ancestors.

Pro tip: Mention that the recursive solution runs in O(n) time and O(h) space, and for the parent-pointer version, you can achieve O(h) time and O(1) space by aligning depths and moving up together. Also, note that if nodes might not be present, you need to handle that case explicitly.

1. Clarify assumptions and edge cases

Ask if the nodes are guaranteed to be in the tree, if a node can be its own ancestor, and if the tree is binary (not necessarily a BST). Also consider if the tree is empty or if one node is the root.

2. Explain the recursive approach for a general binary tree

Describe a post-order traversal: if the current node is null or matches either target, return it. Recursively search left and right subtrees. If both return non-null, the current node is the LCA; otherwise, return the non-null result.

3. Analyze time and space complexity

State that the recursive solution visits each node once, so time complexity is O(n), and space complexity is O(h) due to recursion stack, where h is the tree height.

4. Address the follow-up with parent pointers

Explain that with parent pointers, you can find the LCA by first computing the depths of both nodes, then moving the deeper node up until depths match, and finally moving both up simultaneously until they meet. Alternatively, you can mark ancestors of one node and then traverse up from the other.

5. Compare approaches and discuss trade-offs

Highlight that the parent-pointer approach can be more efficient in terms of space (O(1) extra space if depth is computed iteratively) and may be simpler if parent pointers are already available. Mention that the recursive approach is more general and doesn't require extra pointers.

Key Points to Mention

  • Definition of LCA: the lowest node that has both given nodes as descendants (a node can be a descendant of itself).
  • Recursive post-order traversal: return node if it matches either target, then combine results from left and right subtrees.
  • Handling cases where one node is an ancestor of the other.
  • Time complexity O(n) for the general tree, O(h) for the parent-pointer version if depths are precomputed.
  • Space complexity: O(h) for recursion, O(1) extra space for parent-pointer approach if done iteratively.
  • Edge cases: empty tree, nodes not present, root as LCA.

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