Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Check Balanced Parentheses

EasyPython00: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

Glean query text can contain parentheses to group search conditions. Implement is_balanced(s) to determine whether every opening parenthesis has a matching closing parenthesis in the correct order.

Only the characters ( and ) affect balance. Any other characters, including letters, spaces, punctuation, and operators, must be ignored.

Formal Specification

Given a string s, return a Boolean:

  • Return True if the parentheses are balanced.
  • Return False if a closing parenthesis appears without a matching opening parenthesis, an opening parenthesis remains unmatched, or the nesting order is invalid.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable ASCII characters
  • Only '(' and ')' participate in validation

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