PPractick

DSA interview question

Balanced Parentheses (Valid Parentheses)

Easy · Data structures · Topics: stack, string

The problem

A code editor highlights unbalanced brackets as you type. Given a string of ()[]{}, decide whether it's valid: every opening bracket is closed by the same type, in the correct order (the most recently opened bracket closes first), and nothing is left open. That "most recently opened closes first" rule is exactly LIFO, so a stack of open brackets solves it in one pass. Push each opener; on a closer, the top of the stack must be its matching opener.

Example: "{[]}" → true; "([)]" → false.

Function signature

bool isValid(const string& s);

Test cases

InputOutputExplanation
"()[]{}"true
"{[]}"trueProperly nested
"(]"falseWrong type
"([)]"falseWrong order
"("falseLeft open
")"falseCloses with nothing open
""true

(Confirm: the string contains only the six bracket characters; an empty string is valid.)

How interviewers phrase it

Clarifying questions to ask

  1. Can other characters appear? (If so, skip them.)
  2. Is an empty string valid? (Yes.)
  3. Only one bracket type? (Then a counter would be enough.)

Follow-ups to expect

  1. "Why a stack?"
  2. "Why isn't a counter enough?"
  3. "What are the failure cases?"
  4. "Quick early exit?"
  5. "Complexity?"

Practise questions like this with a real engineer

On Practick another engineer interviews you live for 60 minutes on a question from this bank. They have the full interviewer guide beside the call: the expected answer to every follow-up, hints to give if you stall, and a scoring rubric. You get the model solution once the session settles. It is free: interview someone for an hour and you have earned your own.

Book a free mock interview

Related questions

All 474 dsa interview questions

DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in