← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Meta software engineering interview with two coding problems back to back. The second one had a follow-up that I did not see coming.

Questions Asked (2)

Q1

Given a binary search tree and a target value, find the node value in the tree that is closest to the target.

Algorithms & Data Structures
Author's notes

Pretty standard BST traversal problem.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use the BST property to guide a search toward the target, keeping track of the closest value seen so far. At each node, update the closest if the current node is nearer to the target, then move left if the target is smaller or right if larger. This yields O(h) time and O(1) space.

Pro tip: Explicitly discuss edge cases like an empty tree, duplicate values, and integer overflow when computing differences. Also, mention that an in-order traversal would give a sorted array but is less efficient than the BST-guided approach.

1. Clarify the problem and constraints

Ask about the tree structure (is it a valid BST?), whether the target can be outside the range of values, and if there are duplicate values. Confirm the expected return type (node value, not node).

2. Outline the BST-guided approach

Explain that you'll traverse from the root, maintaining the closest value found so far. At each node, compare the absolute difference between node value and target, updating the closest if needed.

3. Detail the traversal logic

Describe how to decide the direction: if target < node value, go left; if target > node value, go right; if equal, return immediately. This leverages the BST property to prune the search space.

4. Analyze complexity and edge cases

State that time complexity is O(h) where h is the tree height (O(log n) for balanced, O(n) worst-case), and space is O(1) for iterative. Mention handling empty tree, single node, and target outside range.

5. Discuss alternative approaches

Briefly mention that an in-order traversal to get a sorted list and then binary search would be O(n) time and O(n) space, which is less efficient. Also, note that if the tree is not a BST, a full traversal is needed.

Key Points to Mention

  • BST property: left subtree values are smaller, right subtree values are larger.
  • Maintain closest value and update using absolute difference.
  • Direction decision based on target vs. current node value.
  • Time complexity O(h) and space O(1) for iterative solution.
  • Edge cases: empty tree, target equal to a node, target outside min/max.
  • Avoid integer overflow when computing differences (use long or compare without subtraction).

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

Q2

Given a string with parentheses, remove the minimum number of parentheses to make it valid. Follow-up: can you do it without using a stack?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

The base problem I handled fine, classic stack approach to track unmatched indices then rebuild the string.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the problem and edge cases, then propose a stack-based solution that tracks indices of unmatched parentheses. For the follow-up, explain a two-pass counting method that uses O(1) space by first removing invalid closing parentheses and then invalid opening ones.

Pro tip: Meta interviewers value clean, efficient code and clear communication. Before coding, discuss trade-offs between the stack and two-pass approaches, and mention that the two-pass method is optimal for space but requires careful handling of indices.

1. Clarify the problem

Ask if the input can contain other characters, if the output should be any valid string or a specific one, and confirm that we only remove parentheses.

2. Stack-based solution

Use a stack to store indices of unmatched opening parentheses. When a closing parenthesis is encountered, pop if possible; otherwise, mark it for removal. After traversal, mark remaining stack indices for removal.

3. Two-pass O(1) space solution

First pass: count and remove invalid closing parentheses by tracking balance. Second pass (right-to-left): remove invalid opening parentheses similarly. This uses only counters and modifies the string in place.

4. Analyze complexity and trade-offs

Compare time and space: stack uses O(n) space, two-pass uses O(1) extra space but may require multiple passes. Discuss when each is preferable.

5. Test with examples

Walk through examples like '(()', ')()', and 'a)b(c' to verify correctness and edge cases.

Key Points to Mention

  • Use a stack to track indices of unmatched opening parentheses for the initial solution.
  • For O(1) space, use two passes: left-to-right to remove invalid ')', right-to-left to remove invalid '('.
  • Time complexity is O(n) for both approaches; space complexity is O(n) for stack and O(1) for two-pass.
  • Handle edge cases: empty string, no parentheses, all invalid parentheses.
  • In the two-pass method, use a balance counter and a write pointer to build the result in place.
  • Discuss that the two-pass method may not preserve the original order of removals but yields a valid string.

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