← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Meta SWE coding round, just the one tree problem. Pretty standard stuff but it's the kind of question where you can embarrass yourself if you haven't thought about it carefully before.

Questions Asked (1)

Q1

Given the root of a binary tree, find the length of the longest path between any two nodes in the tree, measured by the number of edges.

Algorithms & Data Structures
Author's notes

The tricky part is that the longest path doesn't have to go through the root, which is easy to forget under pressure.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the height of each subtree while updating a global maximum diameter. At each node, the longest path through it is the sum of the heights of its left and right subtrees; the overall answer is the maximum of this value across all nodes.

Pro tip: Clarify that the diameter is measured in edges, not nodes, and mention that the path may or may not pass through the root. Also, note that the tree could be skewed, so recursion depth might be O(n) and an iterative approach may be needed to avoid stack overflow.

1. Clarify the problem

Confirm that the diameter is the number of edges on the longest path between any two nodes, and that the path does not necessarily pass through the root. Ask about constraints (e.g., tree size) to discuss recursion limits.

2. Define the recursive function

Define a function that returns the height of a subtree (max edges from the node to a leaf). At each node, compute the left and right heights.

3. Update the diameter

At each node, the candidate diameter is left_height + right_height. Update a global maximum with this value.

4. Return the height

Return 1 + max(left_height, right_height) to the parent, representing the height of the current subtree.

5. Analyze complexity

State that the time complexity is O(n) since each node is visited once, and space complexity is O(h) for recursion stack, where h is the tree height (O(n) worst case).

Key Points to Mention

  • Post-order traversal (DFS) to compute subtree heights bottom-up.
  • Global variable to track the maximum diameter across all nodes.
  • Diameter through a node = left height + right height.
  • Height of a node = 1 + max(left height, right height).
  • Time complexity O(n), space complexity O(h) due to recursion stack.
  • Edge cases: empty tree (diameter 0), single node (diameter 0), skewed tree (recursion depth O(n)).

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