Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Validating Brackets with Stack

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

Your question is Validating Brackets 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

Micro1's assessment platform receives bracket sequences that must be checked before further parsing. Given a string containing only (, ), {, }, [, and ], determine whether the sequence is valid using a stack.

A string is valid when every opening bracket is closed by the matching bracket type, brackets close in the correct last-in, first-out order, and no closing bracket appears without a corresponding opening bracket.

Formal Specification

Implement is_valid(s), where s is a string. Return True if the bracket sequence is valid, otherwise return False.

The empty string is considered valid.

Constraints

  • 0 <= len(s) <= 10^4
  • s contains only (, ), {, }, [, and ]
  • The empty string is valid

Function Signature

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