Check whether brackets (), [] and {} are balanced and correctly nested using a stack in O(n). One of the most-asked Python interview questions.
The problem
Return True if every bracket in s — (), [], {} — is closed by the right type, in the right order. Any other character is ignored.
Examples
Example 1
Input
is_balanced('{[()]}')Expected output
True
Example 2
Input
is_balanced('([)]')Expected output
False
Example 3
Input
is_balanced('((')Expected output
False
+ 4 hidden tests on Submit — closer before opener, other characters ignored.
Edge cases to ask about
- Empty string
- Closer first
- Unclosed openers
- Wrong nesting '([)]'
Hints
0/3How an interviewer scores this
0/9Your code runs in real CPython inside your browser — nothing is sent anywhere. The first run downloads the interpreter (about 6 MB, once). Your code is saved on this device as you type.
Complexity Lab
What does this cost as n grows?
Interviewers score the analysis as much as the code. Commit to an answer first — then check it, and measure your code against the optimal one at growing input sizes.
Pick both to reveal the answer.
Measure it
Runs the function on inputs of size 250 up to 16,000 and records the time and peak memory. Slow solutions stop early — a short curve is itself the answer.
From brute force to optimal
The progression an interviewer wants to hear, one step at a time.
| Approach | Time | Space | Idea |
|---|---|---|---|
| Repeatedly delete '()', '[]', '{}' | O(n²) | O(n) | Each replace pass is O(n), up to n/2 passes. |
| bestStack of open brackets | O(n) | O(n) | Push openers; every closer must match the top. |
Walkthrough of the optimal approach (try it yourself first)
Push every opening bracket. On a closing bracket, the stack must be non-empty and its top must be the matching opener — otherwise return False. After the scan, leftover openers mean the string is unbalanced, so return not stack.
A dict from closer to opener keeps the matching logic to one line.
Complexity: O(n) time, O(n) space. One pass; in the worst case (all openers) the stack holds n characters.
Reveal the reference solution
def is_balanced(s): pairs = {")": "(", "]": "[", "}": "{"} stack = [] for ch in s: if ch in "([{": stack.append(ch) elif ch in pairs: if not stack or stack.pop() != pairs[ch]: return False return not stack
Follow-ups interviewers ask
- Return the index of the first error.
- Minimum insertions to balance it.
Frequently asked interview questions
Core interview concepts, complexities, and follow-ups scored by hiring teams.
What is the time complexity of Balanced Parentheses Checker in Python?
The optimal solution runs in O(n) time and O(n) auxiliary space. One pass; in the worst case (all openers) the stack holds n characters.
What is the brute-force approach, and how do you optimise it?
Repeatedly delete '()', '[]', '{}': O(n²) time, O(n) space. Each replace pass is O(n), up to n/2 passes. Stack of open brackets: O(n) time, O(n) space. Push openers; every closer must match the top.
What follow-up questions do interviewers ask about Balanced Parentheses Checker?
Return the index of the first error. Minimum insertions to balance it.
