KatInterview

Validate Nested Tag Expressions with Typed Bracket Rules

Stacks & QueuesAuto-graded · 19 test cases
HardAuto-gradedStacks & Queuesstacksbracket matchingparsing

Last updated

Tests bracket matching using a stack to enforce context-sensitive nesting rules across four distinct bracket types with extra validity constraints.

Nested Tag Expression Validator

You are building a validator for a custom markup language that uses four types of bracket pairs:

OpenCloseType name
()round
[]square
{}curly
<>angle

A string is valid if and only if all of the following rules hold:

  1. Proper nesting: Every opening bracket is closed by the matching closing bracket, in the correct LIFO order (no crossing).

  2. Angle brackets cannot directly contain round brackets.
    If ( appears while the innermost enclosing bracket is <, the expression is invalid.
    (Any depth of angle nesting counts — the check is: is the immediately enclosing bracket <?)

  3. Square brackets must contain at least one pair of curly brackets as a direct child.
    "Direct child" means a bracket pair whose opening bracket appears with the square bracket as the immediately enclosing bracket.
    A square bracket that closes without ever having had a { opened directly inside it is invalid.

  4. Curly brackets may not be nested directly inside another curly bracket.
    If { appears while the immediately enclosing bracket is {, the expression is invalid.

  5. The expression must be fully closed (no unmatched open brackets remain at the end).

  6. Unexpected closing brackets (no matching open, or wrong type) make the expression invalid.

Given a string expr containing only the characters ( ) [ ] { } < > and no other characters, return True if the expression is valid, False otherwise.

Constraints

  • 1 ≤ len(expr) ≤ 2000
  • expr consists solely of the eight bracket characters ()[]{}<>.
  • An empty string is not a valid input (length ≥ 1), but a string with no brackets at length 1 is impossible by the character constraint — all characters are brackets.
  • The empty string edge case (length = 0) will not appear in test inputs, but a string of length 1 is always invalid (a lone bracket cannot be balanced).

Examples

Example 1

Input: validate_tag_expression('[{()}]')

Output: True

Example 2

Input: validate_tag_expression('[{<>}({<[{}]>})]')

Output: True

Example 3

Input: validate_tag_expression('[{}(<>)]')

Output: True

Hints

Hint 1

Use a stack where each entry records the bracket type you pushed. When you see a closing bracket, check both that the top matches and that none of the context-sensitive rules are violated at close time.

Hint 2

For rule 3, you need to track per-stack-frame whether a direct-child `{` has been opened. Consider pushing a small record (bracket type + a flag) rather than just the character.

Hint 3

Process each character once. Rules 2 and 4 fire on the *open* bracket event (check the current top of the stack). Rule 3 fires on the *close* event for `]` (check the flag for that frame before popping).