← Meta Interview Insights

Meta·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Meta SWE coding round, one question on string manipulation with parentheses. Pretty standard stack problem but the output requirements tripped me up a bit.

Questions Asked (1)

Q1

Given a string containing lowercase letters and parentheses, remove the fewest parentheses possible to make the string valid. Return both the count of removals and the resulting string.

Algorithms & Data Structures
Author's notes

My first instinct was to just scan left to right and track unmatched parens with a counter, but then I realized I also needed to reconstruct the actual string, not just count removals.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to track unmatched opening parentheses and a counter for unmatched closing parentheses. After one pass, remove the unmatched closing parentheses and the unmatched opening parentheses (by marking their indices) to produce the valid string. Return the total count of removals and the resulting string.

Pro tip: Clarify whether you need to remove the minimum number of parentheses or if any valid string is acceptable; the stack approach guarantees minimal removals. Also, consider edge cases like empty string or string with no parentheses.

1. Understand the problem

Confirm that you need to remove the fewest parentheses to make the string valid, and return both the count and the resulting string. Ask clarifying questions about input constraints and expected output format.

2. Choose the right data structure

Use a stack to keep track of indices of unmatched opening parentheses. Also maintain a set or boolean array to mark characters to be removed.

3. First pass: identify unmatched parentheses

Iterate through the string. For each '(', push its index onto the stack. For each ')', if the stack is not empty, pop an index (matching pair); otherwise, mark this ')' for removal. After the loop, mark all indices remaining in the stack for removal.

4. Build the result string and count removals

Construct the resulting string by including only characters whose indices are not marked for removal. The number of removals is the size of the marked set.

5. Analyze complexity and test edge cases

State that the time complexity is O(n) and space complexity is O(n). Test with examples like '(()', ')()', '())(', and strings with no parentheses.

Key Points to Mention

  • Use a stack to track unmatched opening parentheses and a counter for unmatched closing parentheses.
  • Mark indices of parentheses to remove to avoid multiple passes or string manipulation.
  • The algorithm runs in O(n) time and O(n) space.
  • Edge cases: empty string, string with only parentheses, already valid string.
  • The solution guarantees the minimum number of removals because it only removes parentheses that cannot be matched.
  • Return both the count and the resulting string as specified.

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