← Eightfoldai Interview Insights

Eightfoldai·Software Engineer·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Interviewed for a software engineering role at Eightfold AI and got a parentheses removal problem. Pretty standard coding round but the follow-ups pushed it further than I expected.

Questions Asked (4)

Q1

Given a string with lowercase letters and parentheses, remove the minimum number of parentheses to make the string valid. A valid string means every closing paren has a matching opener before it and the total count is balanced. Return any valid result.

Algorithms & Data Structures
Author's notes

I knew the stack-based approach going in but fumbled explaining why it works.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Use a stack to track indices of unmatched opening parentheses, and a set to mark indices of parentheses to remove. After one pass, remove all marked characters to produce a valid string.

Pro tip: Clarify that multiple valid answers exist and that your solution returns any one; mention that the algorithm runs in O(n) time and O(n) space, which is optimal.

1. Understand the problem

Restate the problem: remove the minimum number of parentheses to make the string valid. Confirm that only parentheses matter and that any valid result is acceptable.

2. Choose data structures

Use a stack to store indices of unmatched opening parentheses and a set to record indices of parentheses to remove.

3. Single pass with stack

Iterate through the string: push index for '(', pop for ')' if stack is non-empty, otherwise mark that ')' index for removal.

4. Mark unmatched openers

After the pass, all indices remaining in the stack are unmatched '('; add them to the removal set.

5. Build result

Construct the result string by including only characters whose indices are not in the removal set. Return the result.

Key Points to Mention

  • Stack-based approach for matching parentheses
  • Time complexity O(n) and space complexity O(n)
  • Handling of unmatched closing parentheses by marking for removal
  • Handling of unmatched opening parentheses left in the stack
  • Multiple valid outputs and returning any one
  • Edge cases: empty string, all parentheses, no parentheses

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

Q2

Follow-up: instead of returning the string, can you just return the count of minimum removals needed?

Algorithms & Data Structures
Author's notes

Much easier once the main solution is done, basically just return the sum of unmatched opens and closes.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Acknowledge the change in requirements and explain that the core algorithm remains the same, but the return value changes from the modified string to an integer count. Modify the existing solution to track the number of removals instead of building the result string, ensuring the count is accurate and the solution remains efficient.

Pro tip: Show that you understand the trade-offs: returning a count instead of the string can save memory and time, but you must ensure the count is computed correctly without unnecessary string operations. Also, mention that this change might allow for further optimizations, such as early termination or using a counter instead of a stack.

1. Clarify the requirement

Confirm that the interviewer wants only the minimum number of removals, not the resulting string. Ask if the count should be the total removals or the minimum removals to make the string valid.

2. Review the original algorithm

Recall the approach used to find the minimum removals, such as using a stack to track unmatched parentheses. Identify where the count can be incremented instead of modifying the string.

3. Adapt the algorithm

Replace string manipulation with a counter variable. For example, in the stack approach, increment a counter for each unmatched closing parenthesis and add the stack size at the end for unmatched opening parentheses.

4. Analyze complexity

State that the time complexity remains O(n) and space complexity can be reduced to O(1) if using a counter instead of a stack, or O(n) if a stack is still used.

5. Test with examples

Walk through a few examples to verify the count, such as '()())' where removals = 1, and ')((' where removals = 3.

Key Points to Mention

  • The core algorithm (e.g., stack-based or two-pass) remains unchanged; only the return type changes.
  • Use a counter to track removals instead of building the result string, which can save memory.
  • Time complexity stays O(n), and space complexity can be optimized to O(1) with a counter.
  • Handle edge cases: empty string, all opening parentheses, all closing parentheses.
  • Explain the logic for counting unmatched parentheses: increment for each unmatched closing, then add remaining opening parentheses.
  • Mention that this modification might be preferred in scenarios where only the count is needed, improving efficiency.

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

Q3

Follow-up: if there are multiple valid answers with the same minimum removals, how would you return the lexicographically smallest one?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

This is where I stumbled.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify the problem context: typically, we want the lexicographically smallest sequence after removing the minimum number of elements to make it valid (e.g., non-decreasing). Then, explain that we can modify the standard dynamic programming approach to track the lexicographically smallest result among those with minimum removals, or use a greedy algorithm with a stack that prioritizes smaller characters while maintaining the minimum removal count.

Pro tip: Mention that lexicographic order is determined by the first differing character, so we should prioritize making earlier characters as small as possible, even if it means later characters are larger. Also, note that if the problem allows multiple valid answers, we need to define a tie-breaking rule, and lexicographically smallest is a common choice.

1. Clarify the problem and constraints

Confirm what 'minimum removals' means and what makes a sequence valid (e.g., non-decreasing). Ask if the input can contain duplicates and what the expected output format is (string, array, etc.).

2. Identify the standard approach for minimum removals

For example, for making a sequence non-decreasing, the minimum removals equals the length minus the longest non-decreasing subsequence (LNDS). Alternatively, a greedy stack approach can compute the minimum removals directly.

3. Adapt to track lexicographically smallest result

If using DP, store not just the length but also the lexicographically smallest subsequence for each state. If using a greedy stack, when a removal is possible, prefer removing a larger previous character to allow a smaller current character to take its place, ensuring lexicographic minimality.

4. Handle ties and prove correctness

Explain why the adapted algorithm yields the lexicographically smallest among all optimal solutions. Discuss how to compare sequences efficiently (e.g., using string comparison or custom comparators).

5. Analyze complexity and trade-offs

State the time and space complexity of your approach. Mention if there's a trade-off between simplicity and optimality, and whether a simpler approach (like generating all optimal solutions and picking the smallest) is feasible.

Key Points to Mention

  • Definition of lexicographic order: compare sequences element by element from the start.
  • Minimum removals often equates to finding the longest valid subsequence (e.g., LNDS).
  • Dynamic programming can be augmented to store the lexicographically smallest subsequence for each state.
  • Greedy stack approach: use a stack to build the result, and when a removal is needed, pop if the top is greater than the current element and removals remain, but also consider lexicographic impact.
  • Tie-breaking: when multiple optimal solutions exist, choose the one with the smallest first element, then second, etc.
  • Complexity considerations: DP may be O(n^2) or O(n log n) with optimizations; greedy stack is O(n).

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

Q4

Follow-up: can you reduce the extra space used in your solution?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I was using a stack and a separate boolean array to mark deletions.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Acknowledge the current space complexity, then propose a concrete optimization such as in-place modification, two-pointer technique, or using a fixed-size array. Explain the trade-offs (e.g., time vs. space) and confirm the new complexity.

Pro tip: Always clarify the constraints and whether input modification is allowed; this shows you consider practical implications and can lead to a more suitable solution.

1. Restate the problem and current solution

Briefly summarize the problem and your initial approach, highlighting the space complexity and why it uses extra space.

2. Identify optimization opportunities

Analyze if the extra space can be eliminated by reusing input, using pointers, or leveraging properties of the data.

3. Propose an optimized approach

Describe a specific technique (e.g., in-place reversal, two-pointer, bit manipulation) that reduces space, and outline the steps.

4. Analyze trade-offs

Discuss the impact on time complexity, code readability, and any assumptions (e.g., mutable input).

5. Confirm new complexity and edge cases

State the new space complexity (e.g., O(1)) and verify it handles edge cases correctly.

Key Points to Mention

  • Space complexity analysis (Big O notation)
  • In-place algorithms
  • Two-pointer technique
  • Trade-offs between time and space
  • Constraints on input modification
  • Edge cases and correctness

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