Challenge - 5 Problems
Balanced Parentheses Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Balanced Parentheses Check
What is the output of the following Go code that checks if the parentheses in the string are balanced?
DSA Go
package main import "fmt" func isBalanced(s string) bool { stack := []rune{} pairs := map[rune]rune{')': '(', ']': '[', '}': '{'} for _, char := range s { switch char { case '(', '[', '{': stack = append(stack, char) case ')', ']', '}': if len(stack) == 0 || stack[len(stack)-1] != pairs[char] { return false } stack = stack[:len(stack)-1] } } return len(stack) == 0 } func main() { fmt.Println(isBalanced("{[()()]()}")) }
Attempts:
2 left
💡 Hint
Think about how the stack is used to match opening and closing brackets.
✗ Incorrect
The code uses a stack to push opening brackets and pop them when a matching closing bracket is found. The input string is balanced, so the function returns true.
❓ Predict Output
intermediateOutput for Unbalanced Parentheses
What will the output be when running this Go code with the input string "{[(])}"?
DSA Go
package main import "fmt" func isBalanced(s string) bool { stack := []rune{} pairs := map[rune]rune{')': '(', ']': '[', '}': '{'} for _, char := range s { switch char { case '(', '[', '{': stack = append(stack, char) case ')', ']', '}': if len(stack) == 0 || stack[len(stack)-1] != pairs[char] { return false } stack = stack[:len(stack)-1] } } return len(stack) == 0 } func main() { fmt.Println(isBalanced("{[(])}")) }
Attempts:
2 left
💡 Hint
Check if the closing brackets match the last opening bracket in the stack.
✗ Incorrect
The string is not balanced because the closing bracket ')' does not match the last opening bracket '[' in the stack, so the function returns false.
🔧 Debug
advancedIdentify the Error in Parentheses Checking Code
What error will this Go code produce when checking parentheses balance?
DSA Go
package main import "fmt" func isBalanced(s string) bool { stack := []rune{} pairs := map[rune]rune{')': '(', ']': '[', '}': '{'} for _, char := range s { if char == '(' || char == '[' || char == '{' { stack = append(stack, char) } else if char == ')' || char == ']' || char == '}' { if len(stack) == 0 || stack[len(stack)] != pairs[char] { return false } stack = stack[:len(stack)-1] } } return len(stack) == 0 } func main() { fmt.Println(isBalanced("()")) }
Attempts:
2 left
💡 Hint
Check how the stack is accessed when popping elements.
✗ Incorrect
The code tries to access stack[len(stack)] which is out of range because valid indices go from 0 to len(stack)-1.
🧠 Conceptual
advancedStack Usage in Balanced Parentheses Problem
Why is a stack the best data structure to check for balanced parentheses?
Attempts:
2 left
💡 Hint
Think about how brackets must be closed in reverse order of opening.
✗ Incorrect
A stack follows last-in, first-out order, which matches how brackets must be closed in reverse order of opening, making it ideal for this problem.
🚀 Application
expertResulting Stack State After Processing Input
Given the input string "[{()}({})]({})", what is the state of the stack after processing all characters using the balanced parentheses stack algorithm?
Attempts:
2 left
💡 Hint
Visualize pushing opening brackets and popping when matching closing brackets appear.
✗ Incorrect
All brackets in the input string are properly matched and closed, so the stack is empty at the end.
