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 this stack operation sequence?
Consider a stack initially empty. We perform these operations in order:
push(5), push(10), pop(), push(7), pop(), push(3), push(8), pop()
What is the content of the stack from top to bottom after these operations?
push(5), push(10), pop(), push(7), pop(), push(3), push(8), pop()
What is the content of the stack from top to bottom after these operations?
DSA Python
stack = [] stack.append(5) stack.append(10) stack.pop() stack.append(7) stack.pop() stack.append(3) stack.append(8) stack.pop() print(stack)
Attempts:
2 left
💡 Hint
Remember pop removes the top element of the stack.
✗ Incorrect
The stack evolves as follows: push 5 -> [5], push 10 -> [5,10], pop -> removes 10 -> [5], push 7 -> [5,7], pop -> removes 7 -> [5], push 3 -> [5,3], push 8 -> [5,3,8], pop -> removes 8 -> [5,3].
🧠 Conceptual
intermediate1:30remaining
Which statement best describes the LIFO principle in stacks?
Choose the option that correctly explains the Last In First Out (LIFO) principle used in stacks.
Attempts:
2 left
💡 Hint
Think about which element is on top of the stack.
✗ Incorrect
LIFO means the last element pushed onto the stack is the first one to be popped off.
🔧 Debug
advanced1:30remaining
What error does this stack code produce?
Consider this Python code using a list as a stack:
stack = [1, 2, 3]
stack.pop(3)
What error will this code raise when run?
stack = [1, 2, 3]
stack.pop(3)
What error will this code raise when run?
DSA Python
stack = [1, 2, 3] stack.pop(3)
Attempts:
2 left
💡 Hint
Check the valid indices for pop in a list of length 3.
✗ Incorrect
pop(3) tries to remove element at index 3, but valid indices are 0,1,2. This causes IndexError.
❓ Predict Output
advanced2:00remaining
What is the printed output after these stack operations?
Given this code:
stack = []
for i in range(1, 5):
if i % 2 == 0:
stack.append(i)
else:
stack.pop() if stack else None
print(stack)
What is the final content of the stack?
stack = []
for i in range(1, 5):
if i % 2 == 0:
stack.append(i)
else:
stack.pop() if stack else None
print(stack)
What is the final content of the stack?
DSA Python
stack = [] for i in range(1, 5): if i % 2 == 0: stack.append(i) else: stack.pop() if stack else None print(stack)
Attempts:
2 left
💡 Hint
Trace each iteration carefully, noting when pop happens.
✗ Incorrect
i=1 (odd): stack empty, pop skipped -> []
i=2 (even): append 2 -> [2]
i=3 (odd): pop top (2) -> []
i=4 (even): append 4 -> [4]
🚀 Application
expert2:30remaining
How many items remain in the stack after this sequence?
A stack starts empty. We perform these operations:
push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5), push(6), pop()
How many items are left in the stack at the end?
push(1), push(2), push(3), pop(), push(4), pop(), pop(), push(5), push(6), pop()
How many items are left in the stack at the end?
DSA Python
stack = [] stack.append(1) stack.append(2) stack.append(3) stack.pop() stack.append(4) stack.pop() stack.pop() stack.append(5) stack.append(6) stack.pop() print(len(stack))
Attempts:
2 left
💡 Hint
Count pushes and pops carefully step by step.
✗ Incorrect
Operations stepwise:
push 1 -> [1]
push 2 -> [1,2]
push 3 -> [1,2,3]
pop -> removes 3 -> [1,2]
push 4 -> [1,2,4]
pop -> removes 4 -> [1,2]
pop -> removes 2 -> [1]
push 5 -> [1,5]
push 6 -> [1,5,6]
pop -> removes 6 -> [1,5]
Stack size is 2.
push 1 -> [1]
push 2 -> [1,2]
push 3 -> [1,2,3]
pop -> removes 3 -> [1,2]
push 4 -> [1,2,4]
pop -> removes 4 -> [1,2]
pop -> removes 2 -> [1]
push 5 -> [1,5]
push 6 -> [1,5,6]
pop -> removes 6 -> [1,5]
Stack size is 2.