← Meta Interview Insights

Meta·Software Engineer·Onsite - Coding / Algorithms·Intermediate

Intermediate
Jul 2026

Summary

Meta SWE coding round, one problem the whole time: minimum removals to make a parentheses string valid. Clean enough problem but the follow-up is where things get interesting and where I felt a bit underprepared.

Questions Asked (2)

Q1

Given a string with parentheses, remove the minimum number of characters to make it valid. What's your approach?

Algorithms & Data Structures
Author's notes

I went with a stack of indices, single pass, mark the unmatched ones and skip them when building the output.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Clarify the problem first: confirm whether to return the valid string or just the minimum number of removals, and whether multiple valid answers are acceptable. Then propose a two-pass stack-based solution that identifies unmatched parentheses and removes them, achieving O(n) time and O(n) space. Walk through a concrete example to demonstrate correctness and discuss edge cases.

Pro tip: After presenting the stack solution, mention that you can optimize space to O(1) by using a counter for unmatched closing parentheses and a second pass to remove excess opening ones. This shows you think about trade-offs and can adapt to constraints.

1. Clarify requirements and constraints

Ask whether to return the valid string or the count of removals, and if multiple valid outputs are acceptable. Confirm input size and character set (only parentheses or other chars).

2. Outline the stack-based approach

Explain that you'll use a stack to track indices of unmatched opening parentheses and a set to mark unmatched closing parentheses. After one pass, remove all marked characters.

3. Walk through an example

Choose a string like 'a)b(c)d' and demonstrate step-by-step how the stack and set identify unmatched parentheses, then show the resulting valid string.

4. Analyze complexity and edge cases

State time and space complexity (O(n) each). Discuss edge cases: empty string, all parentheses, nested, multiple valid answers, and strings with other characters.

5. Discuss optimizations and alternatives

Mention the O(1) space two-pass counter approach and compare trade-offs. Optionally, note that a greedy approach doesn't work for minimum removals.

Key Points to Mention

  • Use a stack to track indices of unmatched opening parentheses.
  • Use a set or boolean array to mark characters to remove.
  • Time complexity O(n) and space complexity O(n) for the stack solution.
  • Edge cases: empty string, all opening/closing parentheses, nested structures, and strings with other characters.
  • Alternative O(1) space solution using two passes with counters.
  • Clarify whether to return the string or the count, and if multiple valid answers are acceptable.

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

Q2

Now extend your solution to handle multiple bracket types: parentheses, square brackets, and curly braces all at once.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is the part I fumbled a bit.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Generalize the stack-based solution by using a mapping of closing brackets to opening brackets. Iterate through the string, pushing opening brackets onto the stack and, for closing brackets, checking that the stack is non-empty and the top matches the expected opening bracket. At the end, ensure the stack is empty.

Pro tip: Mention that this approach runs in O(n) time and O(n) space, and that using a hash map for bracket pairs makes the solution easily extensible to new bracket types. Also, clarify that you validate the input (e.g., non-null) and handle edge cases like empty strings.

1. Clarify requirements and edge cases

Confirm that the input is a string containing only bracket characters, and discuss edge cases such as empty string, single bracket, and mismatched types.

2. Choose data structure and mapping

Use a stack to track opening brackets, and a hash map to associate each closing bracket with its corresponding opening bracket.

3. Iterate and validate

Traverse the string: if the character is an opening bracket, push it; if it's a closing bracket, check that the stack is not empty and the top matches the expected opening bracket, then pop.

4. Final check and complexity

After iteration, ensure the stack is empty to confirm all brackets are closed. State the time and space complexity: O(n) time and O(n) space.

Key Points to Mention

  • Stack data structure for LIFO matching
  • Hash map for mapping closing brackets to opening brackets
  • Handling mismatched brackets (e.g., '([)]' is invalid)
  • Edge cases: empty string, single bracket, non-bracket characters
  • Time and space complexity analysis
  • Extensibility to additional bracket types

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