Check Whether a String of Three Bracket Types Is Properly Nested
Company: ByteDance
Role: Site Reliability Engineer
Category: Coding & Algorithms
Difficulty: easy
Interview Round: Technical Screen
Overview: A coding question asking whether a string of round, square and curly brackets is properly nested, with every opening bracket closed by the same type in the right order. It tests ordering logic and edge cases such as mismatched types, stray closing brackets and openers that are never closed.
Constraints
- 1 <= len(s) <= 10^4
- s contains only the six characters '(', ')', '[', ']', '{' and '}'.
Examples
Input: ('(',)
Expected Output: False
Explanation: Minimum length: a lone opening bracket is never closed.
Input: (')',)
Expected Output: False
Explanation: Minimum length: a lone closing bracket has nothing to close.
Hints
- Matching counts of each bracket type are not enough on their own: compare "([)]" with "([])".
- When a closing bracket appears, decide which earlier opening bracket it is allowed to close under the reverse-order rule.
- Consider both failure modes in the rules: a closer with nothing left to close, and an opener still unclosed when the string ends.