← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jul 2026

Summary

Meta coding round with two algorithm problems, both pretty standard but the parentheses one had some edge cases I didn't fully think through in time.

Questions Asked (2)

Q1

Given the root of a binary tree, return the values of nodes visible from the right side of the tree, ordered top to bottom.

Algorithms & Data Structures
Author's notes

BFS level-order traversal, grab the last node at each level.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use BFS level-order traversal, recording the last node's value at each level. Alternatively, use DFS prioritizing the right child, tracking depth to capture the first node seen at each new depth. Both approaches yield O(n) time and O(h) space (where h is tree height).

Pro tip: Clarify with the interviewer whether the tree can be empty or skewed, and mention that BFS is more intuitive for level-based problems while DFS uses less memory for balanced trees. Also, discuss how you would handle very large trees where recursion depth might be an issue.

1. Understand the problem

Confirm that 'visible from the right side' means the rightmost node at each depth. Clarify edge cases: empty tree, single node, skewed tree.

2. Choose an approach

Decide between BFS (level-order) and DFS (right-first). Explain the trade-offs: BFS uses a queue and processes level by level; DFS uses recursion/stack and tracks depth.

3. Implement the solution

For BFS: enqueue root, for each level record the last node's value. For DFS: traverse right child first, and when visiting a new depth, add the node's value to the result.

4. Test with examples

Walk through a sample tree (e.g., [1,2,3,null,5,null,4]) and verify the output. Also test edge cases like empty tree and left-skewed tree.

5. Analyze complexity

State time complexity O(n) and space complexity O(h) for DFS or O(w) for BFS (w = max width). Discuss which is more efficient in different scenarios.

Key Points to Mention

  • BFS level-order traversal: process each level and take the last node.
  • DFS with right-first traversal: track depth and add first node seen at each depth.
  • Time complexity: O(n) where n is number of nodes.
  • Space complexity: O(h) for DFS (recursion stack) or O(w) for BFS (queue), where h is height and w is max width.
  • Handling edge cases: empty tree returns empty list, single node returns [root.val].
  • Trade-offs: BFS is intuitive for level-based problems; DFS may use less memory for balanced trees.

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

Q2

Given a string with lowercase letters and parentheses, remove the minimum number of parentheses to make it valid and return any valid result.

Algorithms & Data Structures
Author's notes

This one tripped me up more than I expected.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack-based approach to track unmatched parentheses: first pass marks invalid closing parentheses, second pass marks invalid opening parentheses. Then build the result by including only characters that are not marked for removal. Alternatively, use a counter-based method to achieve the same in two passes.

Pro tip: Clarify that 'minimum removal' means removing only unmatched parentheses, and that multiple valid answers may exist—returning any is acceptable. Also mention that the solution runs in O(n) time and O(n) space, which is optimal.

1. Understand the problem and constraints

Confirm that the input contains lowercase letters and parentheses, and that you need to remove the minimum number of parentheses to make the string valid. A valid string has balanced parentheses and no unmatched ones.

2. Choose an approach

Decide between a stack-based method (to track indices of unmatched parentheses) or a two-pass counter method (to count unmatched closing and opening parentheses). Both are O(n) time and O(n) space.

3. Identify unmatched parentheses

In the first pass, mark or count unmatched closing parentheses. In the second pass (if using stack, after reversing or using another stack), mark unmatched opening parentheses.

4. Construct the result

Build the output string by including only characters that are not marked for removal. Ensure the result is valid and has minimum removals.

5. Test with examples

Walk through examples like 'a)b(c)d' -> 'ab(c)d' and 'lee(t(c)o)de)' -> 'lee(t(c)o)de' to verify correctness and edge cases.

Key Points to Mention

  • Minimum removal means removing only unmatched parentheses, not all parentheses.
  • Stack can store indices of unmatched parentheses for easy removal.
  • Two-pass approach: first pass handles unmatched closing, second pass handles unmatched opening.
  • Time complexity O(n) and space complexity O(n) are optimal.
  • Multiple valid outputs may exist; returning any is acceptable.
  • Edge cases: empty string, all parentheses, no parentheses, nested parentheses.

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