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 C# code that checks if the parentheses in the string are balanced?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBalanced(string s) { Stack<char> stack = new Stack<char>(); foreach (char c in s) { if (c == '(') { stack.Push(c); } else if (c == ')') { if (stack.Count == 0) return false; stack.Pop(); } } return stack.Count == 0; } static void Main() { string input = "(()())"; Console.WriteLine(IsBalanced(input)); } }
Attempts:
2 left
💡 Hint
Think about how the stack is used to track opening parentheses and matched with closing ones.
✗ Incorrect
The code pushes '(' onto the stack and pops when it sees ')'. For the input "(()())", all parentheses are matched, so the stack is empty at the end, returning true.
❓ Predict Output
intermediateOutput for Unbalanced Parentheses Input
What will the output be when the following code runs with input string "(()"?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBalanced(string s) { Stack<char> stack = new Stack<char>(); foreach (char c in s) { if (c == '(') { stack.Push(c); } else if (c == ')') { if (stack.Count == 0) return false; stack.Pop(); } } return stack.Count == 0; } static void Main() { string input = "(()"; Console.WriteLine(IsBalanced(input)); } }
Attempts:
2 left
💡 Hint
Check if all opening parentheses have matching closing ones.
✗ Incorrect
The input "(()" has one opening parenthesis without a matching closing one, so the stack is not empty at the end, returning false.
🧠 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 open and close in nested order.
✗ Incorrect
Parentheses are nested, so the last opening parenthesis must be closed first. A stack's Last-In-First-Out order perfectly models this behavior.
🔧 Debug
advancedIdentify the Bug in Parentheses Checking Code
What error will this code produce when checking the string ")("?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBalanced(string s) { Stack<char> stack = new Stack<char>(); foreach (char c in s) { if (c == '(') { stack.Push(c); } else if (c == ')') { stack.Pop(); } } return stack.Count == 0; } static void Main() { string input = ")("; Console.WriteLine(IsBalanced(input)); } }
Attempts:
2 left
💡 Hint
What happens if you pop from an empty stack?
✗ Incorrect
The code pops from the stack without checking if it is empty. For input ")(", the first character is ')', so it tries to pop from an empty stack causing InvalidOperationException.
🚀 Application
expertBalanced Parentheses with Multiple Types
Given the code below that checks for balanced parentheses, brackets, and braces, what is the output for input "{[(])}"?
DSA C#
using System; using System.Collections.Generic; class Program { static bool IsBalanced(string s) { Stack<char> stack = new Stack<char>(); foreach (char c in s) { if (c == '(' || c == '[' || c == '{') { stack.Push(c); } else { if (stack.Count == 0) return false; char top = stack.Pop(); if ((c == ')' && top != '(') || (c == ']' && top != '[') || (c == '}' && top != '{')) { return false; } } } return stack.Count == 0; } static void Main() { string input = "{[(])}"; Console.WriteLine(IsBalanced(input)); } }
Attempts:
2 left
💡 Hint
Check if the closing brackets match the last opening bracket.
✗ Incorrect
The input "{[(])}" has mismatched brackets: ']' closes '[' but the last opening bracket before ']' is '('. So the function returns false.
