Balancing Parentheses
Reported by candidates from Oracle's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
Oracle's parentheses balancing question shows up in their February assessments, and it's a straightforward stack problem that catches people off guard when they blank on the mechanics. You get a string of brackets, braces, or mixed delimiters, and you need to verify they're balanced and properly nested. It's not hard if you remember the pattern. It's a safety net moment where StealthCoder can feed you the template if your mind goes blank under pressure.
Pattern and pitfall
The trick is a simple stack: push opening delimiters, pop and match on closing ones. If the stack empties cleanly at the end and never tries to pop when empty, you're valid. Common pitfalls include forgetting to check the final stack state, mixing up which bracket type closes which, or not handling empty strings. The pattern is pure stack mechanics. No optimization tricks, no hidden edge cases beyond 'what if the input has no brackets' or 'what if it's all opening.' Under live OA pressure, the logic is bulletproof once you write it, so StealthCoder serves as your memory anchor if you freeze on the exact implementation.
The honest play: practice the pattern, and have StealthCoder ready for the one you didn't see coming.
You can drill Balancing Parentheses cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play.
Get StealthCoderRelated leaked OAs
This OA pattern shows up on LeetCode as valid parentheses. If you have time before the OA, drill that.
You've seen the question.
Make sure you actually pass Oracle's OA.
Oracle reuses patterns across OAs. Built for the candidate who saw this exact problem leak two days before his OA and wondered if anyone had a play. Works on HackerRank, CodeSignal, CoderPad, and Karat.
Balancing Parentheses FAQ
Is this really just a stack problem with no twist?+
Yes. Push opens, pop and match closes, return whether stack ends empty. Oracle doesn't hide complexity here. The OA is testing whether you know the pattern cold, not whether you can invent something clever.
What about mixed delimiters like brackets and braces together?+
Stack handles it. Each closing character must match the most recent opening. If you see a '}' but the top of stack is '[', it fails. The data structure manages nesting automatically.
Do I need to optimize for time or space?+
No. Stack solution is O(n) time and O(n) space in the worst case. That's optimal. Oracle's OA isn't asking for sublinear magic here.
What if the input is an empty string or has no brackets?+
Empty string is balanced. No brackets also balanced. Both return true. Handle these upfront so you don't accidentally fail on edge case thinking.
How do I code this in under 10 minutes during the OA?+
Write a character-to-character loop. Use a stack (or list in Python). Push if open, pop and check if close. Return stack.isEmpty() at the end. Straightforward. Under 20 lines of code.