DSA interview question
A code formatter receives a valid parentheses string made of several primitive groups (non-empty balanced groups that can't be split further), such as (()()) followed by (()). The formatter should strip the outermost pair from each primitive group, keeping everything inside. The neat observation: a parenthesis is outermost exactly when it opens a group at depth 0 or closes one back to depth 0. So a single pass with a depth counter decides for every character whether to keep it, with no stack or splitting needed.
Example: "(()())(())" → "()()()".
string removeOuterParentheses(const string& s); // s is a valid parentheses string
Input s | Output | Explanation |
|---|---|---|
"(()())(())" | "()()()" | Primitives (()()) and (()) become ()() and () |
"(()())(())(()(()))" | "()()()()(())" | Three primitives |
"()()" | "" | Each primitive is just () |
"(())" | "()" | |
"((()))" | "(())" |
(Confirm: the input is always valid; only the outermost pair of each primitive group is removed.)
( and ), or other bracket types too?On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.
Book a free mock interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in