← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Meta SWE coding round, two LeetCode problems back to back. Nothing too exotic but the second one definitely required more graph intuition than I expected.

Questions Asked (2)

Q1

Given a binary tree, find the length of its longest path between any two nodes (the diameter).

Algorithms & Data Structures
Author's notes

Felt pretty manageable.

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; update the global max and return 1 + max(left, right).

Pro tip: Clarify that the diameter may or may not pass through the root, and mention that the O(n) solution is optimal because every node must be visited at least once.

1. Clarify the problem

Confirm that the diameter is the number of edges (or nodes) on the longest path between any two nodes, and that the path may or may not pass through the root. Ask if the tree is binary and if edge weights are uniform.

2. Define the recursive function

Define a helper function that returns the height of a subtree (longest downward path from that node) and updates a global variable for the maximum diameter seen so far.

3. Compute diameter at each node

For a given node, compute the heights of its left and right subtrees. The longest path through this node is left_height + right_height; update the global maximum if this sum is larger.

4. Return height to parent

Return 1 + max(left_height, right_height) to the parent, representing the longest downward path from the current node.

5. Analyze complexity

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

Key Points to Mention

  • The diameter may not pass through the root, so a global maximum is needed.
  • Post-order traversal ensures children are processed before the parent.
  • The height of a null node is 0 (or -1 if counting edges).
  • Time complexity is O(n) and space complexity is O(h) due to recursion.
  • Edge cases: empty tree (diameter 0), single node (diameter 0), skewed tree (diameter equals height).
  • The algorithm can be implemented iteratively with a stack to avoid recursion limits, but recursion is simpler.

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

Q2

Given a binary matrix with exactly two islands, find the minimum number of 0s you need to flip to connect them.

Algorithms & Data Structures
Author's notes

This one tripped me up more than I'd like to admit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Model the problem as a shortest path between the two islands, where the cost of traversing a cell is 1 if it's water (0) and 0 if it's land (1). Use multi-source BFS from one island to compute the minimum number of water cells to flip to reach the other island, or run BFS from both islands simultaneously and track the minimum sum of distances when frontiers meet.

Pro tip: Clarify that flipping a 0 to 1 connects it to adjacent land, so the answer is the minimum number of water cells on any path between the islands, not the Manhattan distance. Mention that you can optimize by only exploring water cells and treating land as cost 0, or by using 0-1 BFS.

1. Identify the two islands

Scan the matrix to find the starting cell of each island (e.g., using DFS/BFS to label connected components).

2. Choose BFS strategy

Decide between multi-source BFS from one island or simultaneous BFS from both. Multi-source BFS from one island is simpler; simultaneous BFS can be more efficient.

3. Run BFS with cost tracking

Use a queue to explore cells, tracking the number of water cells flipped so far. For 0-1 BFS, use a deque and push water cells to the back and land cells to the front.

4. Detect connection and return minimum

When a cell from the other island is reached, return the current cost. If using simultaneous BFS, track the minimum sum of distances when frontiers meet.

5. Analyze complexity

State that time and space complexity are O(m*n) where m and n are matrix dimensions, as each cell is visited at most once.

Key Points to Mention

  • Modeling the problem as a shortest path with 0-1 weights (land cost 0, water cost 1).
  • Using BFS or 0-1 BFS (deque) to efficiently compute minimum flips.
  • Handling the case where islands are already connected (answer 0).
  • Optimization: only consider water cells adjacent to land to reduce search space.
  • Time and space complexity: O(m*n) time and O(m*n) space.
  • Edge cases: islands touching diagonally? (No, only 4-directional connectivity counts).

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