Python exercise · about 5 minutes · free
Stack: Balanced Brackets
Undo buttons, parsers and the call stack all run on one idea: last in, first out.
A stack adds and removes from the same end. In Python a list does it: append() to push, pop() to pop. For each closing bracket, the most recent opener must match.
🌍 Real-life story
Real-life story: compilers, JSON parsers and SQL engines use stacks to check that every opening bracket has a matching close. It is also a top-5 interview question.Your quest: Write
is_balanced(s) returning True if every ( [ { is closed correctly and in order.Starter code
def is_balanced(s):
pass