Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Bracket Matching Program

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

Your question is Bracket Matching Program. 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

Bread Pay checkout content can contain bracketed placeholders and formatting markers. Write a function that determines whether every bracket in a string is correctly matched and nested.

The supported bracket pairs are (), [], and {}. Ignore all non-bracket characters. A string is valid when every opening bracket has a matching closing bracket of the same type, and brackets close in last-in, first-out order.

Formal Specification

Implement is_valid_brackets(s), where s is a string. Return a boolean: True if the bracket sequence is valid, otherwise False.

An empty string or a string containing no brackets is valid.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains printable characters
  • Only (), [], and {} are treated as brackets

Function Signature

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