Reviewed by Aditya Kumar Β· Last reviewed 2026-03-24
Stack: track indices of '('; for ')', pop if stack else mark for removal; remaining stack also removed. Python: stack,remove=[],set(); [stack.append(i) if c=='(' else (stack.pop() if stack else remove.add(i)) for i,c in enumerate(s) if c in '()']; remove.update(stack); return...
Stack: track indices of '('; for ')', pop if stack else mark for removal; remaining stack also removed. Python: stack,remove=[],set(); [stack.append(i) if c=='(' else (stack.pop() if stack else remove.add(i)) for i,c in enumerate(s) if c in '()']; remove.update(stack); return ''.join(c for i,c in enumerate(s) if i not in remove). Or: for i,c: if c=='(': stack.append(i); elif c==')': stack.pop() if stack else remove.add(i); remove.update(stack). O(n) time, O(n) space.
Red Flag: Multiple passes when one suffices. Pro-Move: 'Single pass with stack; build result in second pass to avoid shifting; O(n) both.'
According to DataEngPrep.tech, this is one of the most frequently asked General/Other interview questions, reported at 1 company. DataEngPrep.tech maintains an editor-reviewed database of 1,863 data engineering interview questions across 7 categories.