← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

One coding round at Meta for an infrastructure role, two problems back to back. Nothing too wild but the second one had more edge cases than I expected.

Questions Asked (2)

Q1

Given two nodes in a binary tree where each node has a parent pointer but there's no reference to the root, find their lowest common ancestor.

Algorithms & Data Structures
Author's notes

The trick is you don't have the root, so you can't do the usual recursive thing.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Since there is no root reference, use the parent pointers to traverse upward from each node to the root, storing visited nodes in a hash set. Then traverse from the other node upward until a node is found in the set; that node is the LCA. Alternatively, compute the depths by traversing to the root, align the depths, and move both pointers upward until they meet.

Pro tip: Clarify with the interviewer whether modifying the tree is allowed and whether the nodes are guaranteed to be in the same tree. Also, mention that the hash set approach uses O(h) space, but if space is a concern, the depth alignment method uses O(1) extra space.

1. Clarify assumptions and constraints

Ask if the two nodes are guaranteed to be in the same tree, if parent pointers are valid, and if we can modify the tree. Also, discuss space and time constraints.

2. Choose an approach

Decide between the hash set method (simpler, O(h) space) and the depth alignment method (O(1) space). Explain the trade-offs.

3. Implement the chosen algorithm

For hash set: traverse from node1 to root, storing each node in a set; then traverse from node2 upward until a node is in the set. For depth alignment: compute depths by traversing to root, align depths, then move both pointers until they meet.

4. Analyze complexity

State time complexity O(h) where h is the height of the tree, and space complexity O(h) for hash set or O(1) for depth alignment.

5. Test with edge cases

Consider cases where one node is an ancestor of the other, nodes are the same, or the tree is skewed. Verify the algorithm handles them correctly.

Key Points to Mention

  • Use of parent pointers to traverse upward without needing the root.
  • Hash set to store visited nodes from one path, then check the other path.
  • Depth calculation by traversing to root, then aligning depths to find intersection.
  • Time complexity O(h) and space complexity O(h) or O(1) depending on approach.
  • Handling edge cases: one node is ancestor of the other, nodes are identical, or tree is skewed.
  • Clarifying assumptions: nodes in same tree, parent pointers valid, no cycles.

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

Q2

Given a string containing parentheses and lowercase letters, remove the minimum number of parentheses to make the string valid.

Algorithms & Data Structures
Author's notes

Two passes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify that the goal is to remove the minimum number of parentheses to make the string valid, meaning every closing parenthesis must have a matching opening parenthesis before it. Use a stack to track unmatched opening parentheses and a set to mark unmatched closing parentheses, then build the result by skipping marked characters. Alternatively, use a two-pass counting method to identify invalid parentheses.

Pro tip: Mention that the two-pass counting method uses O(1) space and is more efficient than a stack, but the stack approach is easier to explain and code under pressure. Also, discuss how to handle multiple valid outputs by noting that any valid string with minimum removals is acceptable.

1. Understand the problem

Restate the problem: remove the minimum number of parentheses to make the string valid, where valid means every closing parenthesis has a matching opening parenthesis before it. Confirm that letters are ignored and only parentheses matter.

2. Choose an approach

Decide between stack-based and two-pass counting methods. Explain the trade-offs: stack uses O(n) space but is intuitive; two-pass uses O(1) space but requires careful counting.

3. Identify invalid parentheses

For stack: iterate through string, push indices of '(' onto stack, pop on matching ')', and mark unmatched ')' indices. After iteration, mark remaining '(' indices. For two-pass: first pass left-to-right counts open and marks invalid ')'; second pass right-to-left counts close and marks invalid '('.

4. Build the result

Construct the output string by including only characters whose indices are not marked for removal. Ensure the order of characters is preserved.

5. Analyze complexity and test

State time complexity O(n) and space complexity O(n) for stack or O(1) for two-pass. Walk through edge cases: empty string, all parentheses, no parentheses, nested and sequential parentheses.

Key Points to Mention

  • Definition of a valid parentheses string: every ')' has a matching '(' before it, and no unmatched parentheses remain.
  • Minimum removals: we only remove parentheses that cannot be part of any valid pair.
  • Stack approach: use a stack to track indices of unmatched '(' and a set to mark unmatched ')'.
  • Two-pass counting approach: first pass removes invalid ')', second pass removes invalid '('.
  • Time complexity O(n) and space complexity O(n) for stack, O(1) for two-pass.
  • Handling letters: they are always kept and do not affect validity.

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