Validate Bracket Sequence
Company: WeRide
Role: Software Engineer
Category: Coding & Algorithms
Difficulty: medium
Interview Round: Take-home Project
Quick Answer: This question evaluates proficiency with stack data structures, string parsing and matching logic, and the ability to reason about algorithmic time and space complexity.
Constraints
- 0 <= len(s) <= 100000
- s consists only of the characters ()[]{}
Examples
Input: "()[]{}"
Expected Output: True
Explanation: Each opening bracket is matched by the correct closing bracket in a valid order.
Input: "([{}])"
Expected Output: True
Explanation: The brackets are properly nested, so every closing bracket matches the most recent opening bracket.
Input: "(]"
Expected Output: False
Explanation: The opening `(` is closed by `]`, which is the wrong type.
Input: "([)]"
Expected Output: False
Explanation: Although the counts match, the closing order is incorrect because `)` tries to close `(` before `[` is closed.
Input: "(("
Expected Output: False
Explanation: There are unmatched opening brackets remaining at the end.
Input: ""
Expected Output: True
Explanation: An empty string has no unmatched brackets, so it is valid.
Input: "]"
Expected Output: False
Explanation: A closing bracket appears before any matching opening bracket.
Hints
- Try processing the string from left to right while keeping track of opening brackets that have not been matched yet.
- A stack is useful when you need to match each closing bracket with the most recent unmatched opening bracket.