Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Balanced Brackets Validation
00:00
5 left

Balanced Brackets Validation

EasyPython

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):
Interviewer

Your question is Balanced Brackets Validation. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.