DSA interview question
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.
bool isValid(const string& s);
| Input | Output | Explanation |
|---|---|---|
"()[]{}" | true | |
"{[]}" | true | Properly nested |
"(]" | false | Wrong type |
"([)]" | false | Wrong order |
"(" | false | Left open |
")" | false | Closes with nothing open |
"" | true |
(Confirm: the string contains only the six bracket characters; an empty string is valid.)
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 interviewAll 474 dsa interview questions
DSA mock interviews · System design interview questions · Free mock interview practice · Browse interviewers · Sign in