Remove Invalid Parentheses (BFS)

Data Structures
Hard
Google
25.7K views

Given a string with parentheses, remove the minimum number of invalid parentheses to make the input string valid. Return all possible valid results. Use BFS.

Why Interviewers Ask This

Google asks this question to evaluate a candidate's mastery of graph traversal algorithms, specifically Breadth-First Search, in unstructured string spaces. It tests the ability to handle exponential search trees efficiently while minimizing operations. Interviewers look for candidates who can balance correctness with performance, ensuring they find all valid solutions without redundant computations or infinite loops.

How to Answer This Question

1. Clarify constraints: Confirm if the input contains only parentheses or other characters and define what 'valid' means (balanced pairs). 2. Define the BFS state: Explain that each node in your queue is a string, and edges represent removing one character. 3. Implement level-order processing: Iterate through the current queue level; generate neighbors by removing one invalid parenthesis at a time. 4. Validate early: Check validity immediately upon generation. If valid strings are found in the current level, stop generating further levels to ensure minimum removals. 5. Deduplicate results: Use a Set to store visited strings to prevent re-processing identical states, which is critical for performance on large inputs.

Key Points to Cover

  • Demonstrating understanding that BFS naturally finds the shortest path (minimum removals) in an unweighted graph
  • Explicitly mentioning the use of a Set to prevent revisiting duplicate states and avoid TLE
  • Clarifying the termination condition: stopping immediately after finding valid strings at the current depth
  • Explaining how to generate neighbors by iterating through the string and skipping non-parenthesis characters
  • Acknowledging the worst-case complexity but justifying why BFS is superior to DFS for finding minimum edits

Sample Answer

To solve the Remove Invalid Parentheses problem using BFS, I would first validate a helper function to check if a string has balanced parentheses. The core strategy involves treating every possible string modification as a node in a graph. We start with the initial string in our queue. In each iteration, we process the entire current level of the queue. For every string in this level, we generate all possible next states by removing exactly one parenthesis character. Before adding these new states to the queue, we check if they are valid. If we find any valid strings during this level, we collect them all and immediately terminate the search, ensuring we have removed the minimum number of parentheses. This level-by-level approach guarantees optimality. Crucially, I would use a hash set to track visited strings. Without this, the algorithm could enter an infinite loop or waste time re-processing the same invalid string via different deletion paths. For example, given '()())()', removing the first ')' or the second ')' leads to the same intermediate state. By tracking visited nodes, we ensure efficiency. Finally, we return the list of unique valid strings found at the first successful level. This approach balances finding all solutions with strict adherence to the minimum removal constraint.

Common Mistakes to Avoid

  • Using Depth-First Search (DFS) without pruning, which fails to guarantee the minimum number of removals
  • Failing to deduplicate strings, leading to exponential time complexity and potential runtime errors
  • Not checking validity before adding to the queue, causing unnecessary queue growth
  • Ignoring the requirement to return ALL valid combinations instead of just one solution

Practice This Question with AI

Answer this question orally or via text and get instant AI-powered feedback on your response quality, structure, and delivery.

Start Practicing

Related Interview Questions

Browse all 154 Data Structures questionsBrowse all 87 Google questions