Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
What is the output of the stack after these operations?
Consider a stack implemented using an array with a maximum size of 5. The following operations are performed in order:
push(10), push(20), pop(), push(30), push(40), pop(), push(50)
What is the printed state of the stack (top to bottom) after these operations?
push(10), push(20), pop(), push(30), push(40), pop(), push(50)
What is the printed state of the stack (top to bottom) after these operations?
DSA Python
class Stack: def __init__(self, max_size=5): self.stack = [None] * max_size self.top = -1 self.max_size = max_size def push(self, value): if self.top == self.max_size - 1: return "Stack Overflow" self.top += 1 self.stack[self.top] = value def pop(self): if self.top == -1: return "Stack Underflow" val = self.stack[self.top] self.top -= 1 return val def print_stack(self): if self.top == -1: print("Stack is empty") else: for i in range(self.top, -1, -1): print(self.stack[i], end=" -> " if i != 0 else " -> null\n") s = Stack() s.push(10) s.push(20) s.pop() s.push(30) s.push(40) s.pop() s.push(50) s.print_stack()
Attempts:
2 left
💡 Hint
Remember that pop removes the top element and push adds on top.
✗ Incorrect
After the operations, the stack contains 10 at bottom, then 30, then 50 at top. So printing from top to bottom is 50 -> 30 -> 10 -> null.
❓ Predict Output
intermediate1:30remaining
What error does this stack operation cause?
Given a stack with max size 3, what error will occur after these operations?
push(1), push(2), push(3), push(4)
push(1), push(2), push(3), push(4)
DSA Python
class Stack: def __init__(self, max_size=3): self.stack = [None] * max_size self.top = -1 self.max_size = max_size def push(self, value): if self.top == self.max_size - 1: return "Stack Overflow" self.top += 1 self.stack[self.top] = value s = Stack() s.push(1) s.push(2) s.push(3) result = s.push(4) print(result)
Attempts:
2 left
💡 Hint
Check what happens when you try to push beyond max size.
✗ Incorrect
The stack has max size 3. After pushing 3 elements, the stack is full. The next push returns 'Stack Overflow'.
🔧 Debug
advanced1:30remaining
Why does this pop method cause an error?
This pop method is part of a stack implemented using an array. What error will it cause when popping from an empty stack?
DSA Python
def pop(self): val = self.stack[self.top] self.top -= 1 return val
Attempts:
2 left
💡 Hint
What happens if top is -1 and you try to access stack[top]?
✗ Incorrect
If top is -1 (empty stack), accessing stack[-1] returns last element in Python, but if stack is empty list, it causes IndexError.
❓ Predict Output
advanced2:00remaining
What is the final stack content after these operations?
A stack with max size 4 is empty initially. The following operations are done:
push(5), push(10), pop(), push(15), push(20), push(25), pop(), pop()
Print the stack from top to bottom.
push(5), push(10), pop(), push(15), push(20), push(25), pop(), pop()
Print the stack from top to bottom.
DSA Python
class Stack: def __init__(self, max_size=4): self.stack = [None] * max_size self.top = -1 self.max_size = max_size def push(self, value): if self.top == self.max_size - 1: return "Stack Overflow" self.top += 1 self.stack[self.top] = value def pop(self): if self.top == -1: return "Stack Underflow" val = self.stack[self.top] self.top -= 1 return val def print_stack(self): if self.top == -1: print("Stack is empty") else: for i in range(self.top, -1, -1): print(self.stack[i], end=" -> " if i != 0 else " -> null\n") s = Stack() s.push(5) s.push(10) s.pop() s.push(15) s.push(20) s.push(25) s.pop() s.pop() s.print_stack()
Attempts:
2 left
💡 Hint
Track each push and pop carefully.
✗ Incorrect
After all operations, the stack has 5 at bottom and 15 at top. So printing top to bottom is 15 -> 5 -> null.
🧠 Conceptual
expert1:00remaining
What is the time complexity of push and pop operations in a stack implemented using an array?
Consider a stack implemented using a fixed-size array. What is the time complexity of the push and pop operations?
Attempts:
2 left
💡 Hint
Think about how many steps are needed to add or remove one element at the top.
✗ Incorrect
Both push and pop only change the top index and assign or read one element, so both are constant time O(1).