Reverse Substrings Between Each Pair of Parentheses
A medium-tier problem at 72% community acceptance, tagged with String, Stack. Reported in interviews at Agoda and 2 others.
You're given a string with nested parentheses, and you need to reverse the substring between each pair. Agoda, Flipkart, and Goldman Sachs have asked this. It looks straightforward until you hit nested cases, then the order of operations becomes the trap. A 72% acceptance rate means most people who see it solve it, but that's often because they've drilled stack problems before. If this hits your live OA cold, you might waste 10 minutes on the wrong reversal order. StealthCoder surfaces the working approach instantly, invisible to the proctor.
Companies that ask "Reverse Substrings Between Each Pair of Parentheses"
Reverse Substrings Between Each Pair of Parentheses is the kind of problem that decides whether you pass. StealthCoder reads the problem on screen and surfaces a working solution in under 2 seconds. Invisible to screen share. The proctor sees nothing. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage.
Get StealthCoderThe trick is understanding that you don't reverse in the order parentheses appear in the string. Instead, you process reversals from the innermost pair outward. A stack naturally handles this: push characters onto the stack, and when you hit a closing parenthesis, pop everything back until the matching open paren, then push those characters back in reverse order. The mistake most candidates make is trying to track reversal depth or reversing the entire substring at once, which breaks under nesting. Once you see that a stack gives you the pairing and reversal automatically, the implementation is clean. StealthCoder is the hedge if you blank on why iterative reversal fails here.
Pattern tags
You know the problem.
Make sure you actually pass it.
Reverse Substrings Between Each Pair of Parentheses recycles across companies for a reason. It's medium-tier, and most candidates blank under the timer. StealthCoder is the hedge: an AI overlay invisible during screen share. It reads the problem and surfaces a working solution in under 2 seconds. Built by an engineer who got tired of watching his cohort grind for six months and still get filtered at the OA stage. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Reverse Substrings Between Each Pair of Parentheses interview FAQ
Do I need to handle escaped parentheses or just regular ones?+
Standard problems don't include escape characters. Treat all parentheses as meaningful. Focus on the pairing and reversal logic, not edge cases that aren't tested by major companies asking this.
Is this really a medium, or does the 72% acceptance rate mean it's easier?+
The acceptance rate reflects that people drill stacks in prep. Cold, without that pattern, it's medium because the nested reversal order is non-obvious. Agoda, Flipkart, and Goldman Sachs all report it, so it's real.
Can I solve this without a stack?+
Technically yes, but you'd track depth and indices manually, which is error-prone and slower. A stack solution is cleaner, faster to code, and what the companies expect. Use it.
What's the time and space complexity I should target?+
Linear time and space are standard. Each character is pushed and popped once, and you reverse in place on the stack. Don't overcomplicate it with nested loops or substring copies.
How does this relate to other stack problems I've drilled?+
It's a direct application of stack-based matching and in-place reversal. If you've done valid parentheses or basic stack problems, the pairing logic transfers. The reversal twist is the only new piece.
Want the actual problem statement? View "Reverse Substrings Between Each Pair of Parentheses" on LeetCode →