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 last opening one on the stack. If all match and stack is empty at the end, parentheses are balanced.
Click to reveal answer
beginner
Which characters are typically considered in the balanced parentheses problem?
The characters are usually '(', ')', '{', '}', '[' and ']'. Each opening bracket must be matched with the correct closing bracket.
Click to reveal answer
beginner
What happens if you find a closing parenthesis but the stack is empty?
It means there is no matching opening parenthesis for this closing one, so the parentheses are not balanced.
Click to reveal answer
beginner
Why must the stack be empty at the end of the check for balanced parentheses?
If the stack is not empty, it means there are opening parentheses without matching closing ones, so the parentheses are not balanced.
Click to reveal answer
beginner
How do you check if a closing parenthesis matches the last opening parenthesis on the stack?
Compare the closing parenthesis with the opening one on top of the stack. For example, ')' matches '(', '}' matches '{', and ']' matches '['.
Click to reveal answer
What data structure is best suited for checking balanced parentheses?
AArray
BQueue
CStack
DLinked List
✗ Incorrect
A stack allows last-in, first-out access which is perfect for matching opening and closing parentheses.
What should you do when you encounter an opening parenthesis in the string?
APop from the stack
BIgnore it
CReplace it with a closing parenthesis
DPush it onto the stack
✗ Incorrect
Opening parentheses are pushed onto the stack to be matched later with closing parentheses.
If the stack is empty but you find a closing parenthesis, what does it mean?
AParentheses are balanced
BThere is no matching opening parenthesis
CYou should push the closing parenthesis
DIgnore the closing parenthesis
✗ Incorrect
A closing parenthesis without a matching opening one means the parentheses are not balanced.
What does it mean if the stack is not empty after processing the entire string?
AThere are unmatched opening parentheses
BAll parentheses matched
CThere are unmatched closing parentheses
DThe string is empty
✗ Incorrect
Unmatched opening parentheses remain in the stack, so the parentheses are not balanced.
Which of these pairs correctly matches opening and closing parentheses?
A'{' and '}'
B'(' and '}'
C'[' and '}'
D'(' and ']'
✗ Incorrect
'{' matches with '}', '[' matches with ']', and '(' matches with ')'.
Explain step-by-step how a stack helps to check if parentheses in a string are balanced.
Think about how last opened parentheses must be closed first.
You got /4 concepts.
Describe what conditions cause the parentheses to be considered unbalanced when using a stack.
Consider mismatches and leftover openings.
You got /3 concepts.
Practice
(1/5)
1. What does the Balanced Parentheses problem using a stack check for in a string?
easy
A. Whether every opening bracket has a matching closing bracket in correct order
B. If the string contains only alphabets
C. If the string length is even
D. Whether the string contains any numbers
Solution
Step 1: Understand the problem goal
The Balanced Parentheses problem checks if all opening brackets like '(' have a matching closing bracket ')' in the correct order.
Step 2: Role of stack in checking
A stack helps track opening brackets and ensures each one is closed properly in sequence.
Final Answer:
Whether every opening bracket has a matching closing bracket in correct order -> Option A
Quick Check:
Balanced parentheses = matching pairs in order [OK]
Hint: Stack tracks openings; match with closings in order [OK]
Common Mistakes:
Thinking it checks string length
Confusing with character types
Ignoring order of brackets
2. Which of the following is the correct way to push an element 'x' onto a stack named 'stack' in Python?
easy
A. stack.push(x)
B. stack.append(x)
C. stack.add(x)
D. stack.insert(x)
Solution
Step 1: Recall Python list methods for stack
Python uses list to implement stack; to add an element on top, use append().
Step 2: Check each option
add() and push() are not list methods; insert() adds at specific index, not top.
Final Answer:
stack.append(x) -> Option B
Quick Check:
Use append() to push in Python stack [OK]
Hint: Use append() to push in Python stack [OK]
Common Mistakes:
Using push() which is not a Python list method
Using add() which is for sets
Using insert() without index
3. What will be the output of the following code snippet?
def is_balanced(s):
stack = []
for char in s:
if char == '(': stack.append(char)
elif char == ')':
if not stack: return False
stack.pop()
return not stack
print(is_balanced('(()())'))
print(is_balanced('(()'))
medium
A. True\nFalse
B. False\nTrue
C. True\nTrue
D. False\nFalse
Solution
Step 1: Trace first input '(()())'
Push '(' three times, pop ')' three times, stack empty at end, returns True.
Step 2: Trace second input '(()'
Push '(' twice, one ')' pops one '(', stack not empty at end, returns False.