Recall & Review
beginner
What is the main idea behind using a stack to check for balanced parentheses?
Use a stack to keep track of opening parentheses. For every closing parenthesis, check if it matches the top of the stack. If all match and stack is empty at the end, parentheses are balanced.
Click to reveal answer
beginner
Which characters are pushed onto the stack in the balanced parentheses problem?
Only opening parentheses like '(', '{', and '[' are pushed onto the stack.
Click to reveal answer
beginner
What should happen when a closing parenthesis is encountered during the check?
Pop the top element from the stack and check if it matches the type of closing parenthesis. If it doesn't match or stack is empty, parentheses are not balanced.
Click to reveal answer
beginner
Why must the stack be empty at the end of the parentheses check?
If the stack is not empty, it means there are unmatched opening parentheses left, so the parentheses are not balanced.
Click to reveal answer
intermediate
What is the time complexity of checking balanced parentheses using a stack?
The time complexity is O(n), where n is the length of the string, because each character is processed once.
Click to reveal answer
What data structure is best suited for checking balanced parentheses?
✗ Incorrect
Stack follows Last-In-First-Out, which matches the nested structure of parentheses.
What should you do when you encounter an opening parenthesis in the string?
✗ Incorrect
Opening parentheses are pushed onto the stack to be matched later.
If the stack is empty when a closing parenthesis is found, what does it mean?
✗ Incorrect
A closing parenthesis without a matching opening one means unbalanced parentheses.
Which of these strings is balanced?
✗ Incorrect
Only '({[]})' has matching pairs in correct order.
What is the final check after processing all characters in the string?
✗ Incorrect
An empty stack means all opening parentheses were matched.
Explain step-by-step how a stack is used to check if parentheses in a string are balanced.
Think about how you track opening brackets and match them with closing ones.
You got /4 concepts.
Describe what conditions cause the parentheses to be considered unbalanced when using a stack.
Consider what happens if you find a closing bracket but no matching opening bracket.
You got /3 concepts.
