📖 Scenario: Imagine you are building a simple code editor that checks if the parentheses in a line of code are balanced. Balanced parentheses mean every opening bracket has a matching closing bracket in the correct order.
🎯 Goal: You will create a program that uses a stack to check if a given string of parentheses is balanced or not.
📋 What You'll Learn
Create a stack using a list
Use a loop to process each character in the input string
Push opening brackets onto the stack
Pop from the stack when a matching closing bracket is found
Check if the stack is empty at the end to decide if parentheses are balanced
💡 Why This Matters
🌍 Real World
Checking balanced parentheses is important in code editors and compilers to ensure code syntax is correct.
💼 Career
Understanding stacks and balanced parentheses is a common interview question and a fundamental concept in programming and software development.
Progress0 / 4 steps
1
Create the input string of parentheses
Create a variable called parentheses and set it to the string '(()())'.
DSA Python
Hint
Use single quotes to create the string exactly as shown.
2
Create an empty stack
Create a variable called stack and set it to an empty list [].
DSA Python
Hint
A stack can be created using an empty list in Python.
3
Use a loop to check parentheses using the stack
Use a for loop with variable char to iterate over parentheses. Inside the loop, if char is '(', append it to stack. If char is ')', pop from stack if it is not empty. If stack is empty when trying to pop, set a variable balanced to False and break the loop. After the loop, if stack is empty and balanced is not False, set balanced to True, else set it to False.
DSA Python
Hint
Use stack.append() to add and stack.pop() to remove the last item.
Check if stack is empty before popping to avoid errors.
4
Print if the parentheses are balanced
Print "Balanced" if balanced is True, else print "Not Balanced".
DSA Python
Hint
Use an if statement to print the correct message based on the balanced variable.
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.