PracHub
QuestionsPremiumCoachesLearningGuidesInterview Prep

Quick Overview

This question evaluates proficiency in sequence validation, paired-token matching, and the use of data structures to manage nested elements within strings.

  • medium
  • Bloomberg
  • Coding & Algorithms
  • Software Engineer

Validate whether brackets are properly nested

Company: Bloomberg

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Technical Screen

## Problem: Validate a bracket sequence Given a string `s` consisting only of the characters `'('`, `')'`, `'{'`, `'}'`, `'['`, `']'`, determine whether the string is **valid**. A string is valid if: - Every opening bracket has a corresponding closing bracket of the **same type**. - Brackets are closed in the **correct order** (properly nested). - An empty string is considered valid. ### Input - A string `s` (length `0..10^5`) containing only the six bracket characters. ### Output - Return `true` if `s` is valid, otherwise return `false`. ### Examples - `s = "()"` → `true` - `s = "()[]{}"` → `true` - `s = "(]"` → `false` - `s = "([)]"` → `false` - `s = "{[]}"` → `true` ### Follow-up discussion prompts (as in interview) - What is the time and space complexity? - Can you optimize for space or early termination? - What edge cases would you test?

Quick Answer: This question evaluates proficiency in sequence validation, paired-token matching, and the use of data structures to manage nested elements within strings.

Given a string `s` consisting only of the characters `'('`, `')'`, `'{'`, `'}'`, `'['`, `']'`, determine whether the string is **valid**. A string is valid if: - Every opening bracket has a corresponding closing bracket of the **same type**. - Brackets are closed in the **correct order** (properly nested). - An empty string is considered valid. Return `true` if `s` is valid, otherwise return `false`. **Examples** - `s = "()"` → `true` - `s = "()[]{}"` → `true` - `s = "(]"` → `false` - `s = "([)]"` → `false` - `s = "{[]}"` → `true`

Constraints

  • 0 <= s.length <= 10^5
  • s consists only of the characters '(', ')', '{', '}', '[', ']'

Examples

Input: "()"

Expected Output: true

Explanation: A single matched pair is valid.

Input: "()[]{}"

Expected Output: true

Explanation: Three independent matched pairs in sequence are all valid.

Input: "(]"

Expected Output: false

Explanation: '(' is closed by ']' which is the wrong type.

Input: "([)]"

Expected Output: false

Explanation: Brackets are interleaved, not properly nested: ')' tries to close '[' which is on top.

Input: "{[]}"

Expected Output: true

Explanation: Inner '[]' closes before the outer '{}' — correctly nested.

Input: ""

Expected Output: true

Explanation: Empty string is considered valid.

Input: "("

Expected Output: false

Explanation: Unmatched opening bracket remains on the stack at the end.

Input: ")"

Expected Output: false

Explanation: Closing bracket with nothing on the stack to match.

Input: "((()))"

Expected Output: true

Explanation: Deeply nested but balanced parentheses are valid.

Input: "([{}])"

Expected Output: true

Explanation: All three bracket types nested in matching order are valid.

Hints

  1. Process the string left to right and use a stack of open brackets.
  2. When you hit a closing bracket, it must match the most recently seen unmatched opening bracket — that is exactly the top of the stack.
  3. If the stack is empty when you see a closing bracket (or non-empty when the string ends), the string is invalid. You can return early on the first mismatch.
Last updated: Jun 26, 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

  • Minimize Travel Assignment Cost - Bloomberg (medium)
  • Determine Balloon Popping Time - Bloomberg (medium)
  • Solve meeting and tree problems - Bloomberg (easy)
  • Minimize travel cost with two cities - Bloomberg (easy)
  • Check connectivity between two subway stations - Bloomberg (easy)