Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediate2:00remaining
Why do we use a stack in function calls?
Imagine you are cooking multiple dishes, and you need to remember which step to return to after finishing a sub-step. Which of these best explains why a stack is used in managing function calls in programming?
Attempts:
2 left
💡 Hint
Think about how you would keep track of tasks that depend on finishing other tasks first.
✗ Incorrect
A stack follows Last In First Out (LIFO) order, which matches how function calls return: the last function called must finish before returning to the previous one.
❓ Predict Output
intermediate2:00remaining
Output of Stack Operations
What is the printed output after running this Python code that uses a stack to reverse a list?
DSA Python
stack = [] items = [1, 2, 3, 4] for item in items: stack.append(item) reversed_list = [] while stack: reversed_list.append(stack.pop()) print(reversed_list)
Attempts:
2 left
💡 Hint
Think about what happens when you add items to a stack and then remove them.
✗ Incorrect
The stack stores items in order, but popping removes the last added item first, reversing the list.
🔧 Debug
advanced2:00remaining
Identify the Error in Stack Usage
This code tries to check if parentheses in a string are balanced using a stack. What error will it raise when run?
DSA Python
def check_parentheses(s): stack = [] for char in s: if char == '(': stack.append(char) elif char == ')': stack.pop() return len(stack) == 0 print(check_parentheses('())'))
Attempts:
2 left
💡 Hint
What happens if you try to pop from an empty stack?
✗ Incorrect
If the string has more closing parentheses than opening ones, popping from an empty stack causes an IndexError.
🚀 Application
advanced2:00remaining
Using Stack to Evaluate Postfix Expression
Given the postfix expression '3 4 + 2 * 7 /', what is the final result after evaluating it using a stack?
Attempts:
2 left
💡 Hint
Evaluate step-by-step: push numbers, pop two for operation, push result back.
✗ Incorrect
Postfix evaluation uses a stack to compute operations in order. The expression evaluates to 2.0.
🧠 Conceptual
expert2:00remaining
Why is Stack Preferred for Undo Operations?
In text editors, undo operations are often implemented using a stack. Why is a stack the best choice for this?
Attempts:
2 left
💡 Hint
Think about which action you want to undo first.
✗ Incorrect
Undoing requires reversing the most recent action first, which fits the LIFO nature of stacks.