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?
AQueue
BStack
CArray
DLinked List
✗ 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?
AReplace it with closing parenthesis
BPop from the stack
CIgnore it
DPush it onto the stack
✗ 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?
AThere is no matching opening parenthesis
BStack overflow
CParentheses are balanced
DIgnore the closing parenthesis
✗ Incorrect
A closing parenthesis without a matching opening one means unbalanced parentheses.
Which of these strings is balanced?
A({[]})
B({[)]}
C((())
D(()))
✗ Incorrect
Only '({[]})' has matching pairs in correct order.
What is the final check after processing all characters in the string?
AStack size should be odd
BStack should have one element
CStack should be empty
DStack size should be even
✗ 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.
Practice
(1/5)
1. What is the main purpose of using a stack in the Balanced Parentheses problem?
easy
A. To keep track of opening parentheses and match them with closing ones
B. To count the total number of parentheses in the expression
C. To reverse the order of parentheses in the expression
D. To store only the closing parentheses encountered
Solution
Step 1: Understand the role of stack in parentheses matching
A stack helps to remember the last opening parenthesis to match it with the next closing one.
Step 2: Identify why other options are incorrect
Counting or reversing parentheses does not solve matching pairs; storing only closing parentheses misses the matching logic.
Final Answer:
To keep track of opening parentheses and match them with closing ones -> Option A
Quick Check:
Stack tracks openings for matching = B [OK]
Hint: Stack remembers last opening bracket for matching [OK]
Common Mistakes:
Thinking stack counts parentheses instead of matching
Using stack to store only closing parentheses
Ignoring order of parentheses
2. Which of the following code snippets correctly pushes an opening parenthesis '(' onto a stack named stack in C?
easy
A. push(stack, '(');
B. stack.push('(');
C. stack->push('(');
D. push('(', stack);
Solution
Step 1: Recognize typical C stack push syntax
In C, stack operations are usually functions like push(stack, element).
Step 2: Analyze each option
push(stack, '('); matches the common C function call syntax. Options B and C use dot or arrow notation typical in C++ or OOP, not plain C. push('(', stack); has arguments reversed.
Final Answer:
push(stack, '('); -> Option A
Quick Check:
C stack push syntax = A [OK]
Hint: In C, use push(stack, element) function call [OK]
Common Mistakes:
Using C++ style stack.push() in C
Reversing arguments in push function
Using arrow operator on non-pointer stack
3. What will be the output of the following C code snippet that checks balanced parentheses?
char expr[] = "({[]})";
int result = isBalanced(expr);
printf("%d", result);
Assume isBalanced returns 1 if balanced, 0 otherwise.
medium
A. Runtime error
B. 0
C. Compilation error
D. 1
Solution
Step 1: Analyze the expression "({[]})"
The expression has matching pairs: '(' with ')', '{' with '}', '[' with ']'. All are properly nested.
Step 2: Understand isBalanced function behavior
Since the expression is balanced, isBalanced returns 1, so printf prints 1.
Final Answer:
1 -> Option D
Quick Check:
Balanced expression returns 1 = A [OK]
Hint: Balanced nested pairs return 1 from isBalanced [OK]
Common Mistakes:
Assuming only () pairs are valid
Confusing balanced with equal count
Expecting 0 for nested but balanced
4. Identify the bug in this C function snippet for checking balanced parentheses:
int isBalanced(char* expr) {
Stack s;
for (int i = 0; expr[i] != '\0'; i++) {
if (expr[i] == '(') push(&s, '(');
else if (expr[i] == ')') {
if (isEmpty(&s)) return 0;
pop(&s);
}
}
return isEmpty(&s);
}
medium
A. Return statement should be inside the loop
B. Stack s is not initialized before use
C. pop() is called without checking if stack is empty
D. The function does not check for other types of brackets
Solution
Step 1: Check stack initialization
The stack variable 's' is declared but not initialized, so push/pop operations may fail.
Step 2: Verify other parts
Other options are less critical: checking only () is a limitation but not a bug; pop is called after empty check; return outside loop is correct.
Final Answer:
Stack s is not initialized before use -> Option B
Quick Check:
Uninitialized stack causes errors = D [OK]
Hint: Always initialize stack before push/pop [OK]
Common Mistakes:
Ignoring stack initialization
Assuming pop safe without empty check
Thinking return must be inside loop
5. Given the expression "[{()}](({}))", which of the following modifications to the isBalanced function will correctly handle all types of brackets: (), {}, and []?
hard
A. Ignore brackets inside quotes and only check outer brackets
B. Count total opening and closing brackets separately and compare counts
C. Push opening brackets and check matching closing brackets by popping and comparing types
D. Replace all brackets with parentheses before checking
Solution
Step 1: Understand bracket matching logic
To handle multiple bracket types, push opening brackets and on closing bracket, pop and check if types match.
Step 2: Evaluate other options
Counting brackets ignores order; ignoring brackets inside quotes is unrelated; replacing brackets loses type info.
Final Answer:
Push opening brackets and check matching closing brackets by popping and comparing types -> Option C
Quick Check:
Match types by push/pop for all brackets = C [OK]
Hint: Match each closing bracket with last opening of same type [OK]
Common Mistakes:
Counting brackets without order check
Ignoring bracket types
Replacing brackets loses matching info
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.