Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Validate Bracket Pairing

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

Your question is Validate Bracket Pairing. 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

Columbia Bank's expression validation utility receives strings containing parentheses, braces, and square brackets. Determine whether every opening bracket has the correct closing bracket in the correct order.

Implement is_balanced(expression) and return True when the expression is balanced, or False otherwise. An empty expression is considered balanced. The input contains only the six bracket characters: (, ), {, }, [, and ].

Formal Specification

  • Input: A string expression containing zero or more bracket characters.
  • Output: A Boolean indicating whether the brackets are correctly paired and nested.
  • Each closing bracket must match the most recently opened unmatched bracket.

Constraints

  • 0 <= len(expression) <= 10^5
  • expression contains only (, ), {, }, [, and ]
  • A balanced expression must close brackets in reverse order of opening

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