Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implement Balanced Parentheses

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

Your question is Implement 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

Allscripts Sunrise Clinical Manager may process text expressions containing parentheses, square brackets, and curly braces. Write a function that returns True when every delimiter is correctly matched and nested, and False otherwise.

Ignore all characters that are not delimiters. An empty string or a string containing no delimiters is considered balanced.

Formal Specification

Implement is_balanced(text), where text is a string. Return a boolean:

  1. Opening delimiters (, [, and { must be closed by the corresponding delimiter.
  2. Delimiters must close in last-in, first-out order.
  3. A closing delimiter without a matching opening delimiter makes the string invalid.
  4. All opening delimiters must be closed by the end of the string.

Constraints

  • 0 <= len(text) <= 10^5
  • text contains printable ASCII characters
  • Only (), [], and {} affect the result

Function Signature

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