Skip to content
Skilled.dev

Valid Brackets | Coding Interview Question

Validate bracket ordering with a stack of unmatched openings. Test mismatched types, unfinished openings, and closing-first inputs.

12:37 · Trey Huffine · Published 2025-10-15

Watch on YouTube · Read lesson notes · Open the current lesson

Chapters

  1. 0:00 Introduction
  2. 0:08 Question Prompt
  3. 0:33 Constraints
  4. 1:13 Example
  5. 2:13 Solution Breakdown
  6. 6:59 Solution Summary and Big O
  7. 7:50 Code Walkthrough

Selected start: 0:00. Press play in the video to watch.

Changes since recording

  • The Python solution now returns False for a closing bracket with nothing to match, like ")" or "())".

Use the current lesson code when checking behavior.

Lesson notes

These notes explain the problem and current implementation. They are a reading companion, not a verbatim transcript of the recording.

Bracket validation asks whether every closing bracket matches the latest unmatched opening bracket and whether no opening remains at the end. Parentheses, square brackets, and braces share that nesting rule. The current exercise ignores other characters, so text between brackets does not change the stack state.

Let the stack represent unmatched openings

For each opening bracket, push its type. For a closing bracket, first require a nonempty stack, then compare with the top opening. A mismatch proves failure immediately. A successful match removes the top because that obligation is complete. After the entire input, an empty stack means every opening received a compatible closing.

For ([{}]), the closing brace matches the most recent opening brace, then the closing square bracket matches its opening, and finally the closing parenthesis completes the outer pair. For ([)], the first closing parenthesis encounters an opening square bracket and fails. Counting bracket types would miss that ordering error.

Treat underflow and leftovers as separate failures

The input ) fails because there is no opening to match. The input ( fails later because an opening remains. An implementation that pops before checking emptiness may throw instead of returning false. The corrected Python code handles this underflow case explicitly, including a closing bracket after an otherwise completed prefix.

An empty string is valid under the balanced-bracket predicate because it contains no unmatched brackets. For ordinary ignored text, validity follows the same rule. If a different application needs to understand comments, string literals, or escaped brackets, it needs a parser contract beyond this exercise's character scan.

Test nesting, not only pairs

Use nested mixed types, adjacent groups, an early closing bracket, a wrong closing type, and leftover openings. A deep string of openings followed by matching closings exercises stack growth. A nearby single-character mutation should turn a valid string into a rejection without causing an exception.

The scan takes linear time in the input length. Worst-case auxiliary space is linear when many openings accumulate before any closing appears. Read the JavaScript walkthrough for the full implementation and links to the other languages, or open the normal exercise to run your own tests. Explain the meaning of the stack after each input prefix.

More in Stacks and Queues