How do you check if a string of brackets is valid?
PICTURE THIS: STACK VS QUEUE
Simple meaning
Push opening brackets and pop when you see a matching closer
WHY — Stack instead of guessing?
Why interviewers care about Stack:
question about Stack.
trade-offs, and what you would actually do on a DSA project - not buzzwords.
Name the idea, why it exists, then one short example.
End with when you use it and one common pitfall.
STEPS — What happens step by step?
Before you speak the answer, walk the interviewer through these steps:
- 1Push opening brackets and
pop when you see a matching closer
- 2reject on mismatch or
leftover opens.
- 3One O(n) pass and
O(n) stack space in the worst case of all opens.
- 4A counter works only
for one bracket type
- 5Common mistake
mixed types need a stack.
- 6Close
When you pick this over the alternative.
EXAMPLE — See it in action
Here's a short line you can speak, broken into clear beats:
Note: Adapt this scaffold to your own project — keep it under 60–90 seconds.
Key takeaway
Push opening brackets and pop when you see a matching closer reject on mismatch or leftover opens.