Balanced Parentheses Check

viaGlassdoor

Problem: Given a string containing brackets (e.g., '(', ')', '{', '}', '[', ']'), determine whether the brackets are balanced (every opening bracket has a matching closing bracket in the correct order).

Constraints: String may contain other characters that should be ignored, depending on the interviewer's exact framing.

Example: "{[()]}" -> balanced; "{[(])}" -> not balanced.

Approach: Use a stack - push opening brackets; on encountering a closing bracket, pop the stack and check it matches the corresponding opening bracket type (return false on mismatch or empty stack). String is balanced if the stack is empty after processing all characters. O(n) time, O(n) space.

Add a follow-up question they asked
No follow-ups yet. Be the first to add one.
asked …
LeaderboardSalary
Language
Account