← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Senior

SeniorPrefer not to say
May 2026

Summary

Meta SWE coding round focused on tree problems, specifically the diameter/longest path family. The interviewer pushed through multiple variants of the same core DFS idea, which I wasn't fully expecting.

Questions Asked (3)

Q1

Find the diameter of a binary tree, i.e. the longest path (in edges) between any two nodes.

Algorithms & Data Structures
Author's notes

The classic mistake I almost made: I started writing the DFS to return the diameter directly.

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, and the diameter is the maximum of all such paths.

Pro tip: Clarify whether the diameter is measured in edges or nodes, and mention that the path may or may not pass through the root. Also, discuss handling edge cases like an empty tree or a single node.

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 may not pass through the root.

2. Define the recursive function

Design a function that returns the height of a subtree (max edges from root to leaf) and updates a global variable for the maximum diameter.

3. Compute diameter at each node

At each node, calculate the sum of the heights of its left and right subtrees. This represents the longest path passing through that node.

4. Update global maximum

Compare the current node's diameter with the global maximum and update if larger.

5. Return the height

Return 1 + max(left height, right height) to the parent, and finally return the global maximum diameter.

Key Points to Mention

  • Time complexity: O(n) because each node is visited once.
  • Space complexity: O(h) for recursion stack, where h is the height of the tree.
  • The diameter is the maximum of (left diameter, right diameter, left height + right height).
  • Use a global variable or a mutable object to track the maximum diameter during recursion.
  • Handle edge cases: empty tree (diameter 0), single node (diameter 0).
  • The path may not pass through the root, so we must consider all nodes.

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

Q2

Extend the binary tree diameter solution to a general N-ary tree where each node can have any number of children.

Algorithms & Data Structures
Author's notes

Meta apparently loves this as a follow-up and yeah, they asked it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Generalize the binary tree diameter algorithm by computing, for each node, the two largest depths among its children's subtrees. The diameter is the maximum of (sum of top two depths + 1) over all nodes, and the function returns the maximum depth for parent computations.

Pro tip: Emphasize that the core idea remains the same—tracking the two longest downward paths—but the implementation must handle an arbitrary number of children, so using a list or heap to find the top two depths is key. Also, mention that the diameter may or may not pass through the root, so you must consider all nodes.

1. Clarify the problem and constraints

Confirm that the diameter is the number of edges on the longest path between any two nodes, and that the tree is N-ary with no parent pointers. Ask about input size to discuss recursion depth and potential iterative solutions.

2. Define the recursive function

Design a function that returns the maximum depth (in edges) from the current node down to a leaf. At each node, compute the depths of all children's subtrees.

3. Compute top two depths per node

For each node, find the two largest depths among its children. The longest path through this node has length (largest + second largest + 2) if there are at least two children, or (largest + 1) if only one child.

4. Update global diameter

Maintain a global variable for the maximum diameter seen so far. At each node, update it with the path length computed from the top two depths.

5. Return the maximum depth

Return 1 + the largest child depth (or 0 if leaf) to the parent, enabling the parent to compute its own top two depths.

Key Points to Mention

  • The diameter is the maximum of (sum of two largest child depths + 2) over all nodes, with special handling for nodes with fewer than two children.
  • Use a depth-first search (DFS) post-order traversal to compute depths bottom-up.
  • Maintain a global variable to track the maximum diameter across all nodes.
  • The time complexity is O(N) where N is the number of nodes, as each node is visited once.
  • Space complexity is O(H) for recursion stack, where H is the tree height, which could be O(N) in the worst case.
  • Edge cases: empty tree (diameter 0), single node (diameter 0), and nodes with only one child.

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

Q3

What if instead of longest path by edge count, you needed the longest strictly monotonic path in a BST, where node values must be sorted along the path?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Blindsided me a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First clarify the definition of 'strictly monotonic' (increasing or decreasing) and whether the path must be a simple downward path (from ancestor to descendant) or can go up and down. Then propose a DFS that, for each node, computes the longest increasing and decreasing paths starting at that node, combining them appropriately if paths can bend at a node. Analyze time and space complexity, and discuss trade-offs between recursive and iterative implementations.

Pro tip: Mention that in a BST, an in-order traversal yields sorted values, so a strictly increasing path corresponds to a sequence of nodes visited in in-order—this insight can simplify the problem and impress the interviewer.

1. Clarify the problem

Ask whether the path must be strictly increasing, strictly decreasing, or either; whether it must be a simple downward path (ancestor to descendant) or can change direction; and whether the path can skip levels.

2. Define state and recurrence

For each node, define dp_inc[node] = longest strictly increasing path starting at node going downward, and dp_dec[node] = longest strictly decreasing path starting at node going downward. Recurrence: dp_inc[node] = 1 + max(dp_inc[child] for child with value > node.value), similarly for dp_dec.

3. Handle paths that bend

If paths can change direction at a node, the longest monotonic path through a node is dp_inc[node] + dp_dec[node] - 1 (if both directions are allowed) or max(dp_inc[node], dp_dec[node]) if only one direction. Track the global maximum during DFS.

4. Implement and analyze

Write a recursive DFS that returns both dp_inc and dp_dec for each node, updating a global max. Analyze time complexity O(n) and space O(h) for recursion stack, where h is tree height.

5. Discuss trade-offs and edge cases

Consider iterative implementation to avoid recursion depth issues, handle empty tree, single node, and duplicate values (strictness). Discuss whether the BST property can be leveraged for optimization.

Key Points to Mention

  • Definition of strictly monotonic (increasing or decreasing) and whether both are allowed.
  • Path direction: downward only vs. can bend at a node.
  • Dynamic programming state: longest increasing/decreasing path starting at each node.
  • Recurrence relation and how to combine child results.
  • Time and space complexity: O(n) time, O(h) space.
  • Edge cases: empty tree, single node, duplicates, and skewed trees.

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