Balanced Brackets
Problem
Our configuration loader accepts expressions containing three kinds of brackets: (), [] and {}. Write a function that returns true if every opening bracket is closed by the matching kind in the correct order, and false otherwise. Any other characters in the string should be ignored. An empty string is balanced.
This is the warm-up on the Dell Egypt online assessment; the graders check the tricky orderings, not just the happy path.
Examples
Example 1
Input: "{ if (x[0]) { y = [1, 2]; } }"
Output: true — every bracket closes in LIFO order.
Example 2
Input: "([)]"
Output: false — ) arrives while [ is still the most recent open bracket.
Example 3
Input: "(("
Output: false — unclosed brackets at the end are not balanced.
Constraints
- 0 ≤ length ≤ 10^5
- Must be O(n) time; O(n) auxiliary space is acceptable
- Do not use regular expressions that repeatedly strip pairs — that is O(n²)
What they look for
A stack, a map from closers to openers, the early exit on a mismatch, and remembering to check the stack is empty at the end. Bonus if you say how you would return the index of the first offending bracket.