Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Bracket Balance Check

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

Your question is Bracket Balance Check. 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

Codvo.ai expression tooling receives strings containing brackets that must be syntactically valid before further processing. Given a string, determine whether every opening bracket is closed by the correct bracket type and whether brackets are properly nested.

The supported bracket pairs are (), [], and {}. The string may also contain non-bracket characters, which should be ignored. Return True when the brackets are balanced and False otherwise.

Formal Specification

  • Input: s, a string containing printable characters.
  • Output: A boolean. Return True if all brackets are correctly matched and nested; otherwise return False.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Supported bracket pairs are (), [], and {}
  • An empty string is considered balanced

Function Signature

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