Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Check Balanced Parentheses

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

Your question is Check Balanced Parentheses. 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

A Comcast Xfinity Stream interface receives configuration expressions containing grouping symbols. Write a function that determines whether every bracket is correctly matched and nested.

The input may contain parentheses (), square brackets [], and curly braces {}. Non-bracket characters, including letters, digits, spaces, and punctuation, must be ignored. A string is balanced only when every opening bracket has the correct closing bracket in last-in, first-out order.

Formal Specification

Implement is_balanced(s), which accepts a string s and returns a boolean:

  • Return True if all brackets are properly matched and nested.
  • Return False if a closing bracket has no matching opener, if bracket types are mismatched, or if any opener remains unmatched.
  • The input contains standard ASCII characters.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains only ASCII characters
  • Only (), [], and {} affect the result
  • An empty string is 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