Skip to the page

DSA · Build it

Valid parentheses (LeetCode 20)

The interviewer asks

Given a string of brackets, ()[]{}, return whether it's balanced.

Then

Follow-ups the interviewer asks next

  1. Ignore non-bracket characters.
  2. Minimum removals to make it valid.
  3. Validate nested HTML tags like <div><span></span></div>.
  4. Longest valid substring.

The answer

What a strong answer covers

  • a stack and a closer → opener map; push openers, on a closer compare with the top; valid only if the stack is empty at the end; odd-length early exit; O(n) time, O(n) space; for HTML tags, parse tag names and skip void and self-closing tags.

The red pen

Common traps

Avoid:counting brackets only (fails on "([)]"); popping an empty stack; forgetting the final empty check.

Practise this out loud

Say your answer out loud to an AI interviewer that follows up, and get a score for it.

Practise by voice

Practise questions like this out loud

Sign up and start with a 15-minute interview with the full report, and an English conversation. No card needed.

Start your free interview