Remove Outermost Parentheses

EasyStacksimulation

Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.

Practice this as a mock interviewFree to start, no account needed

Problem

A string s of ( and ) characters is a valid parentheses string. Split it into its **primitive** pieces: the shortest non-empty consecutive chunks that are each balanced on their own. Every valid string splits into primitives in exactly one way. Remove the first and last character (the outermost pair) of each primitive piece, then join the pieces back together in their original order and return the result. The result may be the empty string.

Examples

Input: s = "(()())(())"
Output: "()()()"
Primitives are `(()())` and `(())`; stripping their outer pairs gives `()()` and `()`.
Input: s = "(()())(())(()(()))"
Output: "()()()()(())"
Primitives `(()())`, `(())`, `(()(()))` become `()()`, `()`, `()(())`.
Input: s = "()()"
Output: ""
Both primitives are `()`, and removing their outer pair leaves nothing.

Constraints

  • 2 <= s.length <= 10^5
  • `s[i]` is `(` or `)`
  • `s` is a valid parentheses string

What the interviewer grades

A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.

More simulation problems