easyStackAmazonGoogleMicrosoft
Bracket Balance Audit
A linter inspects a punctuation string containing `()`, `[]`, and `{}`. Every opener must be closed in the correct order.
Given a string of `()[]{}`, decide whether every open bracket is closed by the matching type in the right order.
Choose approach
Input
(
)
[
]
{
}
top
1/8
Narrationstep 1 of 8
Walk the string. Push every opener; on a closer, the stack top must match or we reject.
Variables
i=0
stack.size=0
How to think about it
- Brackets must match last-in-first-out — a stack is the natural fit.
- Push every opener.
- On a closer, the top of the stack must be the matching opener.
- If the stack is empty or the top mismatches → reject.
- After scanning, the stack must be empty.
Edge cases to catch in interview
- Empty string → vacuously balanced.
- Only closers → immediate reject.
- Only openers → stack non-empty at end → reject.
Examples
Example 1
true{"text":"()[]{}"}
Example 2
false{"text":"(]"}
Example 3
true{"text":"([{}])"}
Try your own input
SpacePlay / pause
← / →Previous / next step
RReset to start
. or >Cycle speed