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 for Simple Input
What is the output of this Rust code that checks if parentheses are balanced in the string "()[]{}"?
DSA Rust
fn is_balanced(s: &str) -> bool { let mut stack = Vec::new(); for ch in s.chars() { match ch { '(' | '[' | '{' => stack.push(ch), ')' => if stack.pop() != Some('(') { return false; }, ']' => if stack.pop() != Some('[') { return false; }, '}' => if stack.pop() != Some('{') { return false; }, _ => {} } } stack.is_empty() } fn main() { let input = "()[]{}"; println!("{}", is_balanced(input)); }
Attempts:
2 left
💡 Hint
Think about whether all opening brackets have matching closing brackets in the correct order.
✗ Incorrect
The input string "()[]{}" has all types of brackets properly opened and closed in order. The stack pushes opening brackets and pops them when matching closing brackets appear. Since the stack is empty at the end, the function returns true.
❓ Predict Output
intermediateOutput for Unbalanced Parentheses Input
What is the output of this Rust code when checking the string "([)]" for balanced parentheses?
DSA Rust
fn is_balanced(s: &str) -> bool { let mut stack = Vec::new(); for ch in s.chars() { match ch { '(' | '[' | '{' => stack.push(ch), ')' => if stack.pop() != Some('(') { return false; }, ']' => if stack.pop() != Some('[') { return false; }, '}' => if stack.pop() != Some('{') { return false; }, _ => {} } } stack.is_empty() } fn main() { let input = "([)]"; println!("{}", is_balanced(input)); }
Attempts:
2 left
💡 Hint
Check if the closing brackets match the most recent opening bracket.
✗ Incorrect
The input "([)]" is not balanced because the closing bracket ')' does not match the last opening bracket '['. The function returns false when this mismatch is detected.
🔧 Debug
advancedIdentify the Error in Parentheses Checking Code
What error will this Rust code produce when checking the string "(()" for balanced parentheses?
DSA Rust
fn is_balanced(s: &str) -> bool { let mut stack = Vec::new(); for ch in s.chars() { match ch { '(' | '[' | '{' => stack.push(ch), ')' => if stack.pop() != Some('(') { return false; }, ']' => if stack.pop() != Some('[') { return false; }, '}' => if stack.pop() != Some('{') { return false; }, _ => {} } } stack.is_empty() } fn main() { let input = "(()"; println!("{}", is_balanced(input)); }
Attempts:
2 left
💡 Hint
Consider what happens if there are leftover opening brackets after processing the string.
✗ Incorrect
The input "(()" has one extra opening bracket without a matching closing bracket. The stack will not be empty at the end, so the function returns false. No panic occurs because pop is only called when a closing bracket is found.
🧠 Conceptual
advancedWhy Use a Stack for Balanced Parentheses?
Why is a stack the best data structure to check for balanced parentheses in a string?
Attempts:
2 left
💡 Hint
Think about how parentheses must close in the reverse order they open.
✗ Incorrect
A stack follows last-in, first-out order, which matches how parentheses must be closed in reverse order of opening. This makes it ideal for checking balanced parentheses.
🚀 Application
expertResult of Checking Complex Nested Parentheses
What is the output of this Rust code checking the string "{[()()]()}" for balanced parentheses?
DSA Rust
fn is_balanced(s: &str) -> bool { let mut stack = Vec::new(); for ch in s.chars() { match ch { '(' | '[' | '{' => stack.push(ch), ')' => if stack.pop() != Some('(') { return false; }, ']' => if stack.pop() != Some('[') { return false; }, '}' => if stack.pop() != Some('{') { return false; }, _ => {} } } stack.is_empty() } fn main() { let input = "{[()()]()}"; println!("{}", is_balanced(input)); }
Attempts:
2 left
💡 Hint
Check carefully the nesting and matching of all brackets in the string.
✗ Incorrect
The string "{[()()]()}" is properly nested with all opening brackets matched by the correct closing brackets in order. The stack ends empty, so the function returns true.
