Loading section...

Balanced Parentheses: The Stack Problem

Concepts: pyBalancedBrackets, pyStackParsing

This is the single most common beginner stack problem. It shows up in FAANG phone screens, DE interviews, and platform engineering rounds. Given a string of brackets, determine if it is balanced. Balanced means every open bracket has a corresponding close bracket in the correct order. The stack solution is elegant: push open brackets, pop and validate on close brackets. If the stack is empty at the end, it is balanced. Step Through It Carefully Walk through '([{}])' character by character. Push '('. Push '['. Push '{'. See '}' — pop '{', matching['}'] is '{', stack top was '{', they match. See ']' — pop '[', match succeeds. See ')' — pop '(', match succeeds. Stack is empty. Return True. Now walk through '([)]': Push '('. Push '['. See ')' — stack top is '[', matching[')'] is '(', they do N