Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Open and Closed Brackets Checker

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Open and Closed Brackets Checker. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

HCLTech automation tooling may need to validate configuration expressions before processing them. Given a string containing parentheses, square brackets, curly braces, and other characters, determine whether every bracket is correctly matched and nested.

A bracket sequence is valid when each closing bracket matches the most recently opened unmatched bracket. Non-bracket characters must be ignored. Return True for a valid sequence and False otherwise.

Formal Specification

Implement is_valid_brackets(s), where s is a string. Return a boolean indicating whether all brackets in s are balanced and properly ordered. The supported bracket pairs are (), [], and {}.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Only (), [], and {} are treated as brackets
  • Non-bracket characters must be ignored

Function Signature

def is_valid_brackets(s):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output