Quick Overview

This question evaluates understanding of stack data structures, string parsing, and matching logic for balanced brackets and parentheses. It is commonly asked in technical interviews within the Coding & Algorithms domain to assess the ability to produce correct and efficient bracket-matching logic, testing practical application of data-structure implementation alongside conceptual understanding of stack behavior and edge-case handling.

Validate a bracket string

Company: ByteDance

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

Given a string `s` containing only the characters `'('`, `')'`, `'{'`, `'}'`, `'['`, and `']'`, determine whether the input string is valid. A string is valid if: 1. Every opening bracket has a corresponding closing bracket of the same type. 2. Brackets are closed in the correct order. 3. Every closing bracket matches the most recent unmatched opening bracket. Return `true` if `s` is valid; otherwise return `false`. Example: - Input: `"()[]{}"` → Output: `true` - Input: `"([)]"` → Output: `false`

Overview: This question evaluates understanding of stack data structures, string parsing, and matching logic for balanced brackets and parentheses. It is commonly asked in technical interviews within the Coding & Algorithms domain to assess the ability to produce correct and efficient bracket-matching logic, testing practical application of data-structure implementation alongside conceptual understanding of stack behavior and edge-case handling.

Given a string `s` containing only the characters `'('`, `')'`, `'{'`, `'}'`, `'['`, and `']'`, determine whether the input string is valid. A string is valid if: 1. Every opening bracket has a corresponding closing bracket of the same type. 2. Brackets are closed in the correct order. 3. Every closing bracket matches the most recent unmatched opening bracket. Return `true` if `s` is valid; otherwise return `false`. Example: - Input: `"()[]{}"` → Output: `true` - Input: `"([)]"` → Output: `false`

Constraints

  • 1 <= s.length <= 10^4 (the empty string is also considered valid).
  • s consists only of the characters '(', ')', '{', '}', '[' and ']'.

Examples

Input: ("()[]{}",)

Expected Output: True

Explanation: Each opening bracket is immediately closed by a matching closing bracket of the same type.

Input: ("([)]",)

Expected Output: False

Explanation: The ')' tries to close '[', the most recent unmatched opener, so the order is wrong.

Hints

  1. Use a stack. Push every opening bracket as you encounter it.
  2. When you see a closing bracket, it must match the bracket on top of the stack — if the stack is empty or the top does not match, the string is invalid.
  3. After scanning the whole string, the string is valid only if the stack is empty (no unmatched openers remain).

Validate a bracket string

mediumByteDanceSoftware Engineer

Given a string s containing only the characters '(', ')', '{', '}', '[', and ']', determine whether the input string is valid.

A string is valid if:

  1. Every opening bracket has a corresponding closing bracket of the same type.
  2. Brackets are closed in the correct order.
  3. Every closing bracket matches the most recent unmatched opening bracket.

Return true if s is valid; otherwise return false.

Example:

  • Input: "()[]{}" → Output: true
  • Input: "([)]" → Output: false

Examples

Example 1
Input
("()[]{}",)
Output
True
Notes
Each opening bracket is immediately closed by a matching closing bracket of the same type.
Example 2
Input
("([)]",)
Output
False
Notes
The ')' tries to close '[', the most recent unmatched opener, so the order is wrong.

Constraints

  • •1 <= s.length <= 10^4 (the empty string is also considered valid).
  • •s consists only of the characters '(', ')', '{', '}', '[' and ']'.

Comments (0)

No comments yet. Be the first to comment!

Submit your answer

Approved first answer today earns 30 XP
Bold⌘BItalic⌘IInline code⌘`Bullet listTab to nest (○ ■)Numbered listTab to nest (a. i.)QuoteCode block
Formatting help
Signature
def solution(s) -> bool:
Sample call
solution("()[]{}")
Language
Case 1
Input
("()[]{}",)
Expected
True
Case 2
Input
("([)]",)
Expected
False

Browse More Questions