Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Balanced Brackets Validation

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

Your question is Balanced Brackets Validation. 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

NetApp ONTAP configuration and access expressions can contain nested grouping symbols alongside identifiers, operators, and whitespace. Implement a validator that determines whether every bracket is correctly opened, nested, and closed.

Formal Specification

Given a string expression, return True if all parentheses (), square brackets [], and curly braces {} are balanced. Ignore all characters that are not brackets. A closing bracket is valid only when it matches the most recently opened unmatched bracket.

The input is a string, and the output is a Boolean. An empty string or a string containing no brackets is balanced.

Constraints

  • 0 <= len(expression) <= 10^5
  • expression contains printable ASCII characters
  • Only parentheses, square brackets, and curly braces affect the result
  • Non-bracket characters must be ignored

Function Signature

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