KatInterview

Validate Nested Expressions with Typed Bracket Scopes

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

Last updated

Tests bracket matching with complex rules: typed pairs, mandatory ordered sub-structure inside scopes, and precedence-aware nesting constraints.

Validate Nested Expressions with Typed Bracket Scopes

You are given a string representing a structured expression used by a configuration language. The language has three types of bracket pairs:

OpenCloseType
()round
[]square
{}curly

The string contains only these six bracket characters. A string is valid if and only if all of the following rules hold:

Rule 1 — Basic matching

Every opening bracket must be closed by the correct closing bracket of the same type, and every closing bracket must have a matching opener. No unmatched brackets are allowed.

Rule 2 — Nesting hierarchy

Brackets obey a strict containment hierarchy:

  • A curly { } scope may directly contain round ( ) or square [ ] scopes (but not another curly scope directly — curly inside curly must be separated by a round or square scope).
  • A square [ ] scope may directly contain round ( ) scopes only (no square or curly directly inside a square).
  • A round ( ) scope may not directly contain any other scope (it must be empty or contain only other characters — but since the alphabet is only brackets, a round scope must be empty).

"Directly contains" means the inner bracket pair's opener appears as the immediate next token inside the outer scope, with no other unclosed bracket in between at the same depth level.

Rule 3 — Curly scope structure

Every curly scope must contain at least one directly-nested child scope (round or square). An empty curly {} is invalid.

Rule 4 — Square scope structure

A square scope may be empty [] or contain one or more directly-nested round ( ) children. Each directly-nested child must be a round scope.

Rule 5 — Top-level constraint

At the top level (outside all brackets), only curly scopes may appear. Any round or square bracket that opens at the top level makes the expression invalid.


Given a string s consisting solely of the characters ()[]{}, return True if the expression is valid according to all the rules above, and False otherwise.

Constraints

  • 1 ≤ len(s) ≤ 2000
  • s consists only of the characters (, ), [, ], {, }
  • An empty string is not a valid input (length ≥ 1 is guaranteed), but a single character is possible.

Examples

Example 1

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

Output: False

Example 2

Input: validate_expression('{}')

Output: False

Example 3

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

Output: False

Hints

Hint 1

Use a stack to track the current open bracket and its type; when you encounter a closing bracket, check both that it matches the top of the stack and that the nesting hierarchy rule (curly > square/round, square > round only, round > nothing) is respected.

Hint 2

Track, for each stack frame, how many direct children have been opened inside it so far. When closing a curly scope, verify that at least one child was present; when closing a square scope, verify every direct child opened inside it was a round scope.

Hint 3

Handle the top-level as a virtual 'frame' that only accepts curly openers — you can model this by seeding your stack with a sentinel frame that only allows curly children, then at the end verify the sentinel's child count is consistent.