0
0
DSA Pythonprogramming~20 mins

Balanced Parentheses Problem Using Stack in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Balanced Parentheses Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of Balanced Parentheses Check
What is the output of the following code when checking the string "{[()]}" for balanced parentheses?
DSA Python
def is_balanced(s):
    stack = []
    pairs = {')': '(', ']': '[', '}': '{'}
    for char in s:
        if char in '([{' :
            stack.append(char)
        elif char in ')]}' :
            if not stack or stack.pop() != pairs[char]:
                return False
    return not stack

print(is_balanced("{[()]}") )
ATrue
BFalse
CSyntaxError
DNone
Attempts:
2 left
💡 Hint
Think about how the stack is used to match opening and closing brackets.
Predict Output
intermediate
2:00remaining
Output for Unbalanced Parentheses
What will the function return when checking the string "{[(])}" for balanced parentheses?
DSA Python
def is_balanced(s):
    stack = []
    pairs = {')': '(', ']': '[', '}': '{'}
    for char in s:
        if char in '([{' :
            stack.append(char)
        elif char in ')]}' :
            if not stack or stack.pop() != pairs[char]:
                return False
    return not stack

print(is_balanced("{[(])}") )
AFalse
BNone
CIndexError
DTrue
Attempts:
2 left
💡 Hint
Check if the order of brackets is correct in the string.
🔧 Debug
advanced
2:00remaining
Identify the Error in Parentheses Checking Code
What error will this code produce when run with input ")()[]{}"?
DSA Python
def check_balanced(s):
    stack = []
    pairs = {')': '(', ']': '[', '}': '{'}
    for char in s:
        if char in '([{' :
            stack.append(char)
        elif char in ')]}' :
            if stack.pop() != pairs[char]:
                return False
    return not stack

print(check_balanced(")()[]{}"))
AFalse
BTypeError
CTrue
DIndexError
Attempts:
2 left
💡 Hint
What happens if you pop from an empty stack?
Predict Output
advanced
2:00remaining
Output of Balanced Parentheses with Extra Characters
What is the output of the code when checking the string "a+(b*c)-{d/e}" for balanced parentheses?
DSA Python
def is_balanced(s):
    stack = []
    pairs = {')': '(', ']': '[', '}': '{'}
    for char in s:
        if char in '([{' :
            stack.append(char)
        elif char in ')]}' :
            if not stack or stack.pop() != pairs[char]:
                return False
    return not stack

print(is_balanced("a+(b*c)-{d/e}"))
ASyntaxError
BFalse
CTrue
DNone
Attempts:
2 left
💡 Hint
Ignore characters that are not brackets.
🧠 Conceptual
expert
2:00remaining
Stack Usage in Balanced Parentheses Algorithm
Why is a stack the best data structure to check for balanced parentheses in a string?
ABecause a stack automatically removes all brackets when a closing bracket is found.
BBecause a stack allows checking the most recent opening bracket first, matching the last opened with the first closed.
CBecause a stack stores all brackets and sorts them alphabetically for easy matching.
DBecause a stack can store the count of brackets without tracking their order.
Attempts:
2 left
💡 Hint
Think about the order in which brackets must be matched.