← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Meta SWE coding round with two tree problems. Nothing too exotic but the diameter modification tripped me up more than I expected.

Questions Asked (2)

Q1

Given a binary tree, return its vertical order traversal (nodes grouped by column, top to bottom within each column).

Algorithms & Data Structures
Author's notes

Felt okay about this one.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a BFS traversal while tracking the horizontal distance (column) of each node, storing nodes in a map from column to list of node values. Then sort the columns and within each column, ensure nodes are ordered top-to-bottom (BFS naturally gives this if we process level by level).

Pro tip: Clarify the ordering of nodes within the same column and row: if two nodes share the same column and row, they should be ordered left-to-right. Mention that BFS with level-order processing handles this correctly.

1. Clarify requirements and edge cases

Ask about ordering within columns (top-to-bottom, and left-to-right for same row/column) and confirm output format. Discuss edge cases like empty tree, single node, skewed tree.

2. Choose traversal and data structures

Decide on BFS (level-order) to ensure top-to-bottom ordering. Use a hash map (or TreeMap) to group nodes by column index, and a queue for BFS that stores nodes along with their column index.

3. Implement BFS with column tracking

Initialize queue with root at column 0. While queue not empty, process nodes level by level: for each node, add its value to the map entry for its column, then enqueue left child with column-1 and right child with column+1.

4. Collect and sort results

After BFS, extract keys (columns) from the map, sort them in ascending order, and for each column, append its list of values to the result. If using TreeMap, iteration is already sorted.

5. Analyze complexity and test

State time complexity O(N log N) due to sorting columns (or O(N) with TreeMap if columns are bounded), space O(N). Walk through a small example to verify correctness.

Key Points to Mention

  • Use BFS to ensure top-to-bottom ordering within each column.
  • Track horizontal distance (column index) for each node.
  • Use a hash map or TreeMap to group nodes by column.
  • Sort columns before output (or use TreeMap for automatic sorting).
  • Handle edge cases: empty tree, single node, skewed tree.
  • Time and space complexity analysis.

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

Q2

Find the diameter of a binary tree, but return the count of nodes along the longest path rather than the number of edges.

Algorithms & Data Structures
Author's notes

The off-by-one adjustment from edges to nodes sounds trivial and it kind of is, but I fumbled the wording when explaining it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a post-order DFS that returns the height (in nodes) of each subtree while updating a global maximum with the sum of left and right heights plus one. Clarify that the diameter is measured in nodes, so the base case for a null node returns 0 and the path length through a node is leftHeight + rightHeight + 1.

Pro tip: Explicitly state that the diameter may not pass through the root, and that the global maximum must be updated at every node. Also mention that the algorithm runs in O(n) time and O(h) space, which is optimal for this problem.

1. Clarify the definition

Confirm that the diameter is the number of nodes on the longest path between any two nodes, and that the path may or may not pass through the root.

2. Define the recursive function

Design a helper function that returns the height of a subtree in terms of nodes (null returns 0). At each node, compute the left and right heights.

3. Update the global maximum

At each node, the longest path through that node has length leftHeight + rightHeight + 1. Update a global variable with the maximum of this value.

4. Return the height

Return 1 + max(leftHeight, rightHeight) to the parent, representing the height of the current subtree in nodes.

5. Analyze complexity

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

Key Points to Mention

  • The diameter is measured in nodes, not edges, so the base case for a null node returns 0 and the path length through a node is leftHeight + rightHeight + 1.
  • The longest path may not pass through the root, so a global maximum must be updated at every node.
  • Use post-order DFS to compute subtree heights and update the diameter simultaneously.
  • Time complexity is O(n) because each node is visited once; space complexity is O(h) due to recursion stack.
  • Handle edge cases: empty tree returns 0, single node returns 1.
  • Avoid recomputing heights by combining the height calculation and diameter update in one traversal.

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