← Eightfold AI Interview Insights

Eightfold AI·Software Engineer·Technical Phone Screen·Intermediate

IntermediatePrefer not to say
Jun 2026

Summary

Interviewed for a software engineer role at Eightfold AI and got a string manipulation problem that looks straightforward until you actually sit down and think through the edge cases.

Questions Asked (1)

Q1

Given a string containing '(', ')', and lowercase letters, remove the minimum number of parentheses to make the string valid. Return any valid result.

Algorithms & Data Structures
Author's notes

I knew the stack-based approach pretty quickly but fumbled explaining why it's minimum removals.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a two-pass stack-based approach: first pass marks invalid closing parentheses, second pass marks invalid opening parentheses, then build the result excluding marked characters. Alternatively, use a counter-based method to track balance and remove excess parentheses. Clearly explain the algorithm, analyze time and space complexity, and discuss edge cases.

Pro tip: After presenting your solution, mention that the problem can be solved in O(n) time and O(1) space with a two-pass counter method, showing you understand space optimization. Also, clarify that any valid result is acceptable, so you don't need to find the minimum number of removals explicitly.

1. Clarify the problem

Confirm that the input string contains only '(', ')', and lowercase letters, and that we need to remove the minimum number of parentheses to make it valid. Ask if multiple valid outputs are acceptable.

2. Choose an approach

Decide between stack-based and counter-based methods. Explain the trade-offs: stack uses O(n) space but is straightforward; counter method uses O(1) space but requires two passes.

3. Walk through the algorithm

For stack approach: iterate through string, push indices of '('; on ')', if stack not empty, pop and mark both as valid, else mark ')' as invalid. Then mark any remaining '(' in stack as invalid. Build result from unmarked characters.

4. Analyze complexity

State that time complexity is O(n) and space complexity is O(n) for stack, or O(1) for counter method. Mention that the output string is built in O(n) time.

5. Test with examples

Run through examples like 'a)b(c)d' -> 'ab(c)d' and '))((' -> '' to verify correctness. Discuss edge cases: empty string, all parentheses, no parentheses.

Key Points to Mention

  • Two-pass approach: first pass removes invalid closing parentheses, second pass removes invalid opening parentheses.
  • Stack-based solution: push indices of '(' and pop on matching ')', marking unmatched parentheses for removal.
  • Counter-based solution: use balance counters to identify excess parentheses in O(1) space.
  • Time complexity O(n) and space complexity O(n) or O(1) depending on approach.
  • Edge cases: empty string, string with only parentheses, string with no parentheses, nested and sequential parentheses.
  • Any valid result is acceptable, so multiple correct outputs exist.

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