Skip to content

45. Valid Brackets

Difficulty: Easy · Topic: DSA for data engineers

Write is_balanced(text): return True if every bracket in text ((), [], {}) is closed by the matching kind, in the right order. Other characters are ignored, so it works on SQL or JSON snippets.

Your code runs in CPython on our servers against 6 tests, including hidden ones.

Sample test

is_balanced("select sum(x) from t where y in (1, 2) and z = '[a]'")

Expected: True

Hints

Hint 1Push every opening bracket on a stack (a list).
Hint 2A closing bracket must match the bracket on top of the stack; pop it.
Hint 3At the end the stack must be empty.

Learn the concepts

More Python problems

All Python problems