← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Meta SWE coding round, just one algorithmic problem on binary trees. Pretty standard for what they throw at you, though the problem has more edge cases than it looks like at first glance.

Questions Asked (1)

Q1

Given a binary tree, find the maximum path sum where a path can start and end at any node in the tree.

Algorithms & Data Structures
Author's notes

My first instinct was to just do a DFS and track the running max, which is the right direction, but I kept second-guessing whether the path had to go through the root.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the maximum gain from each subtree while updating a global maximum path sum. At each node, compute the best path through the node as node.val + max(0, leftGain) + max(0, rightGain), and return node.val + max(0, max(leftGain, rightGain)) to the parent.

Pro tip: Clarify that a path can start and end at any node, so you must consider paths that pass through a node and connect its left and right subtrees. Also, mention that negative values are handled by taking max(0, gain) to avoid including them.

1. Clarify the problem

Confirm that a path can start and end at any node, may not pass through the root, and that a single node is a valid path. Ask about constraints (e.g., node values can be negative).

2. Define recursive function

Define a helper function that returns the maximum gain from a subtree rooted at a given node, where gain is the maximum sum of a path starting at that node and going down to any node in its subtree.

3. Compute gains and update global max

For each node, recursively compute left and right gains. Ignore negative gains by taking max(0, gain). Update the global maximum with node.val + leftGain + rightGain.

4. Return the best single-branch gain

Return node.val + max(leftGain, rightGain) to the parent, since a path cannot branch when going up.

5. Analyze complexity

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

Key Points to Mention

  • Post-order traversal (DFS) to process children before parent
  • Global variable to track the maximum path sum found so far
  • Handling negative values by taking max(0, gain) to avoid reducing the sum
  • The difference between the path sum through a node (left + node + right) and the gain returned to the parent (node + max(left, right))
  • Time complexity O(n) and space complexity O(h) due to recursion
  • Edge cases: empty tree, single node, all negative values

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