Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Bracket Validation With Stack

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

Your question is Bracket Validation With Stack. 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

Availity Essentials may represent claim validation rules as text containing nested grouping symbols. Given a string, determine whether all bracket characters are correctly matched and properly nested.

The input may contain parentheses (), square brackets [], and curly braces {}. Characters that are not brackets should be ignored. A string is valid when every opening bracket has the correct closing bracket, brackets close in last-in, first-out order, and no unmatched brackets remain.

Implement valid_brackets(s) and return True for valid input or False otherwise.

Formal Specification

  • Input: s, a string with length n, containing printable characters.
  • Output: A Boolean indicating whether the bracket sequence is valid.
  • Only (), [], and {} affect validity. All other characters are ignored.

Constraints

  • 0 <= len(s) <= 10^5
  • s contains ASCII printable characters
  • Only (), [], and {} affect validity
  • Non-bracket characters must be ignored

Function Signature

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