Katkat

Validate bracket sequence

IntuitSoftware Engineer · Technical Screen

Last updated

This question evaluates understanding of bracket matching and string parsing, testing competency with basic data structures and algorithmic reasoning such as stack-based matching and correct handling of nested delimiters.

Given a string s containing only the characters (, ), [, ], {, and }, determine whether the brackets are properly matched.

A string is valid if:

  • Every opening bracket is closed by a bracket of the same type.
  • Brackets are closed in the correct order.
  • No closing bracket appears without a matching earlier opening bracket.

Return true if the string is valid and false otherwise.

Example 1:

  • Input: s = "()[]{}"
  • Output: true

Example 2:

  • Input: s = "([)]"
  • Output: false

Example 3:

  • Input: s = "{[]}"
  • Output: true

Be prepared to write and run a few test cases, including edge cases such as the empty string.