Find the Minimum Parenthesis Deletions and Their Positions

Quick Overview

Return the source indices for a minimum set of parenthesis deletions that leaves a valid sequence, choosing the lexicographically smallest index list when several minimum sets exist.

Find the Minimum Parenthesis Deletions and Their Positions

Company: DoorDash

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Onsite

## Problem Given a string containing only `(` and `)`, remove the minimum number of characters needed to make the remaining string a valid parenthesis sequence. Return the zero-based source indices to remove in ascending order. The answer's length is the minimum deletion count. When several minimum sets exist, return the lexicographically smallest index list. ### Function Contract Implement `minimumParenthesisDeletionIndices(text)` and return an integer array. ### Constraints & Assumptions - `0 <= len(text) <= 200,000`. - `text` contains only left and right parentheses. - A valid sequence never has more closing than opening parentheses in any prefix and has equal totals at the end. - Deleting no characters returns an empty array. ### Clarifying Questions to Ask - Are replacements or insertions allowed? No. - Should the repaired string also be returned? No, only source indices. - How are multiple minimum deletion sets resolved? Lexicographically by their sorted index lists. - Does the empty result string count as valid? Yes. ```hint Unmatched closing positions are forced During a left-to-right scan, a closing parenthesis with no unmatched opening before it must be deleted. ``` ```hint Retain opening indices until they match Store unmatched opening positions. After the scan, those still stored are the excess openings; choosing the earliest unmatched ones follows naturally from ordinary stack matching. ``` ### Examples - `")("` returns `[0, 1]`. - `")(()())("` returns `[0, 7]`. - `")()"` returns `[0]`. - `"(()"` returns `[0]`, the lexicographically smaller of the two one-deletion repairs. - `"()()"` returns `[]`. ### Evaluation Focus - Identifies forced unmatched closing parentheses and leftover opening parentheses. - Returns source indices, sorted, with the stated deterministic tie-break. - Uses `O(n)` time and at most `O(n)` auxiliary space. - Does not allocate or compare every possible repaired string. ### Extensions to Discuss 1. How would you return the repaired string alongside the indices? 2. What changes when letters between parentheses must be preserved? 3. How would multiple bracket types affect minimum deletion?

Quick Answer: Return the source indices for a minimum set of parenthesis deletions that leaves a valid sequence, choosing the lexicographically smallest index list when several minimum sets exist.

|Home/Coding & Algorithms/DoorDash
DoorDash logo
DoorDash
May 26, 2026, 12:00 AM
mediumSoftware EngineerOnsiteCoding & Algorithms
0
0

Problem

Given a string containing only ( and ), remove the minimum number of characters needed to make the remaining string a valid parenthesis sequence.

Return the zero-based source indices to remove in ascending order. The answer's length is the minimum deletion count. When several minimum sets exist, return the lexicographically smallest index list.

Function Contract

Implement minimumParenthesisDeletionIndices(text) and return an integer array.

Constraints & Assumptions

  • 0 <= len(text) <= 200,000 .
  • text contains only left and right parentheses.
  • A valid sequence never has more closing than opening parentheses in any prefix and has equal totals at the end.
  • Deleting no characters returns an empty array.

Clarifying Questions to Ask Guidance

  • Are replacements or insertions allowed? No.
  • Should the repaired string also be returned? No, only source indices.
  • How are multiple minimum deletion sets resolved? Lexicographically by their sorted index lists.
  • Does the empty result string count as valid? Yes.

Examples

  • ")(" returns [0, 1] .
  • ")(()())(" returns [0, 7] .
  • ")()" returns [0] .
  • "(()" returns [0] , the lexicographically smaller of the two one-deletion repairs.
  • "()()" returns [] .

Evaluation Focus

  • Identifies forced unmatched closing parentheses and leftover opening parentheses.
  • Returns source indices, sorted, with the stated deterministic tie-break.
  • Uses O(n) time and at most O(n) auxiliary space.
  • Does not allocate or compare every possible repaired string.

Extensions to Discuss

  1. How would you return the repaired string alongside the indices?
  2. What changes when letters between parentheses must be preserved?
  3. How would multiple bracket types affect minimum deletion?

Submit Your Answer to Earn 20XP

Sign in to leave a comment

Loading comments...