PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency with stack data structures, string parsing and matching logic, and the ability to reason about algorithmic time and space complexity.

  • medium
  • WeRide
  • Coding & Algorithms
  • Software Engineer

Validate Bracket Sequence

Company: WeRide

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Take-home Project

Given a string containing only the characters `()[]{}`, determine whether the brackets are properly matched. A bracket string is considered valid if: - Every opening bracket has a corresponding closing bracket of the same type. - Brackets close in the correct order. - Every closing bracket matches the most recent unmatched opening bracket. Return `true` if the string is valid, otherwise return `false`. Examples: - `"()[]{}"` -> `true` - `"([{}])"` -> `true` - `"(]"` -> `false` - `"([)]"` -> `false` - `"(("` -> `false` Discuss the expected time and space complexity of your approach.

Quick Answer: This question evaluates proficiency with stack data structures, string parsing and matching logic, and the ability to reason about algorithmic time and space complexity.

Given a string `s` containing only the characters `()[]{}`, determine whether the bracket sequence is valid. A bracket string is considered valid if: - Every opening bracket has a corresponding closing bracket of the same type. - Brackets close in the correct order. - Every closing bracket matches the most recent unmatched opening bracket. Return `True` if the string is valid, otherwise return `False`. Examples: - `"()[]{}"` -> `True` - `"([{}])"` -> `True` - `"(]"` -> `False` - `"([)]"` -> `False` - `"(("` -> `False`

Constraints

  • 0 <= len(s) <= 100000
  • s consists only of the characters ()[]{}

Examples

Input: "()[]{}"

Expected Output: True

Explanation: Each opening bracket is matched by the correct closing bracket in a valid order.

Input: "([{}])"

Expected Output: True

Explanation: The brackets are properly nested, so every closing bracket matches the most recent opening bracket.

Input: "(]"

Expected Output: False

Explanation: The opening `(` is closed by `]`, which is the wrong type.

Input: "([)]"

Expected Output: False

Explanation: Although the counts match, the closing order is incorrect because `)` tries to close `(` before `[` is closed.

Input: "(("

Expected Output: False

Explanation: There are unmatched opening brackets remaining at the end.

Input: ""

Expected Output: True

Explanation: An empty string has no unmatched brackets, so it is valid.

Input: "]"

Expected Output: False

Explanation: A closing bracket appears before any matching opening bracket.

Hints

  1. Try processing the string from left to right while keeping track of opening brackets that have not been matched yet.
  2. A stack is useful when you need to match each closing bracket with the most recent unmatched opening bracket.
Last updated: Apr 19, 2026

Loading coding console...

PracHub

Master your tech interviews with 8,000+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.

Related Coding Questions

  • Implement Several Core Algorithmic Components - WeRide (medium)
  • Implement matrix multiplication and fast exponentiation - WeRide (medium)
  • Implement expression expansion to plus-only form - WeRide (Medium)
  • Expand algebraic expression with distribution - WeRide (Medium)