Question bank › Stack
Dsa Stack Medium

Priority Bracket Validator

You have three types of brackets with priorities: '(' (1), '[' (2), '{' (3). A closing bracket matches its opener and 'swallows' (automatically closes) any brackets inside it that have a LOWER priority. However, if it encounters an unclosed bracket of a HIGHER priority inside, the string is 'Invalid'. For example, '[ ( ]' is Valid because ']' closes '[' and swallows the lower-priority '('. But '( [ )' is Invalid because ')' cannot swallow the higher-priority '['. A string must also have all brackets closed by the end. Input is a single string. Output 'Valid' or 'Invalid'.

Key concepts

stacklogic

Practise this out loud — free

Start a mock interview on THIS exact question — a voice AI interviewer opens with it, pushes back like a real onsite, then hands you an instant scorecard.

🎙 Practise this question now
Part of InterviewLab's verified interview question bank. We show the prompt and concepts to practise with — never a copy-paste solution.