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 after pushing elements on the stack?
Consider a stack with a maximum size of 3. We push elements 10, 20, and 30 in order. What will be the printed stack after these operations?
DSA Python
class Stack: def __init__(self, max_size): self.stack = [] self.max_size = max_size def push(self, item): if len(self.stack) < self.max_size: self.stack.append(item) def __str__(self): return ' -> '.join(map(str, self.stack)) + ' -> null' s = Stack(3) s.push(10) s.push(20) s.push(30) print(s)
Attempts:
2 left
💡 Hint
Remember, push adds elements to the top of the stack, but printing shows from bottom to top in this code.
✗ Incorrect
The stack stores elements in order pushed. The __str__ method prints from bottom to top, so the output is 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediate2:00remaining
What does the isEmpty method return after popping all elements?
Given a stack with max size 2, we push 5 and 15, then pop twice. What does isEmpty() return?
DSA Python
class Stack: def __init__(self, max_size): self.stack = [] self.max_size = max_size def push(self, item): if len(self.stack) < self.max_size: self.stack.append(item) def pop(self): if self.stack: return self.stack.pop() return None def isEmpty(self): return len(self.stack) == 0 s = Stack(2) s.push(5) s.push(15) s.pop() s.pop() print(s.isEmpty())
Attempts:
2 left
💡 Hint
Check if the stack has any elements left after popping twice.
✗ Incorrect
After pushing 5 and 15, popping twice removes both elements. The stack is empty, so isEmpty() returns True.
❓ Predict Output
advanced2:00remaining
What error occurs when pushing beyond stack capacity?
What happens when we push an element into a full stack of max size 1?
DSA Python
class Stack: def __init__(self, max_size): self.stack = [] self.max_size = max_size def push(self, item): if len(self.stack) < self.max_size: self.stack.append(item) else: raise OverflowError('Stack is full') s = Stack(1) s.push(100) s.push(200)
Attempts:
2 left
💡 Hint
Check the condition before appending and what happens if the stack is full.
✗ Incorrect
The push method raises OverflowError when trying to add beyond max_size.
❓ Predict Output
advanced2:00remaining
What is the output of isFull after pushing elements?
Given a stack with max size 2, after pushing one element, what does isFull() return?
DSA Python
class Stack: def __init__(self, max_size): self.stack = [] self.max_size = max_size def push(self, item): if len(self.stack) < self.max_size: self.stack.append(item) def isFull(self): return len(self.stack) == self.max_size s = Stack(2) s.push(42) print(s.isFull())
Attempts:
2 left
💡 Hint
Compare current stack size with max_size after one push.
✗ Incorrect
After pushing one element, stack size is 1, max_size is 2, so isFull() returns False.
🧠 Conceptual
expert2:00remaining
Which condition correctly checks if a stack is empty?
You want to write a method isEmpty() for a stack implemented as a list named 'stack'. Which condition correctly returns True if the stack is empty and False otherwise?
Attempts:
2 left
💡 Hint
Think about how to check if a list has no elements.
✗ Incorrect
An empty list has length zero, so checking len(self.stack) == 0 correctly detects emptiness.