Challenge - 5 Problems
Queue Two Stacks Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of enqueue and dequeue operations
What is the printed state of the queue after these operations using two stacks?
DSA Python
class QueueUsingStacks: def __init__(self): self.stack_in = [] self.stack_out = [] def enqueue(self, x): self.stack_in.append(x) def dequeue(self): if not self.stack_out: while self.stack_in: self.stack_out.append(self.stack_in.pop()) if self.stack_out: return self.stack_out.pop() return None q = QueueUsingStacks() q.enqueue(1) q.enqueue(2) q.enqueue(3) q.dequeue() q.enqueue(4) result = [] while True: val = q.dequeue() if val is None: break result.append(val) print(result)
Attempts:
2 left
💡 Hint
Remember that dequeue moves elements from stack_in to stack_out only when stack_out is empty.
✗ Incorrect
After enqueueing 1, 2, 3, the queue is [1, 2, 3]. The first dequeue removes 1. Then enqueue 4 adds it to stack_in. The remaining elements dequeued are 2, 3, 4.
❓ Predict Output
intermediate2:00remaining
Resulting queue after multiple dequeues
What is the output after these operations on a queue implemented with two stacks?
DSA Python
class QueueUsingStacks: def __init__(self): self.stack_in = [] self.stack_out = [] def enqueue(self, x): self.stack_in.append(x) def dequeue(self): if not self.stack_out: while self.stack_in: self.stack_out.append(self.stack_in.pop()) if self.stack_out: return self.stack_out.pop() return None q = QueueUsingStacks() q.enqueue(10) q.enqueue(20) q.enqueue(30) print(q.dequeue()) print(q.dequeue()) q.enqueue(40) print(q.dequeue()) print(q.dequeue())
Attempts:
2 left
💡 Hint
Dequeue empties stack_out before moving elements from stack_in.
✗ Incorrect
Dequeues remove 10 and 20 first. Then enqueue 40. Next dequeue removes 30, then 40.
🔧 Debug
advanced2:00remaining
Identify the error in dequeue method
What error will this code raise when calling dequeue on an empty queue?
DSA Python
class QueueUsingStacks: def __init__(self): self.stack_in = [] self.stack_out = [] def enqueue(self, x): self.stack_in.append(x) def dequeue(self): if not self.stack_out: while self.stack_in: self.stack_out.append(self.stack_in.pop()) return self.stack_out.pop() q = QueueUsingStacks() print(q.dequeue())
Attempts:
2 left
💡 Hint
What happens if you pop from an empty list?
✗ Incorrect
When both stacks are empty, popping from stack_out causes IndexError.
🧠 Conceptual
advanced2:00remaining
Time complexity of dequeue operation
What is the average time complexity of the dequeue operation in a queue implemented using two stacks?
Attempts:
2 left
💡 Hint
Consider how often elements move between stacks.
✗ Incorrect
Each element is moved at most twice: once into stack_in and once into stack_out, so average dequeue is O(1).
❓ Predict Output
expert3:00remaining
Final state of stacks after operations
After executing the following code, what is the content of stack_in and stack_out?
DSA Python
class QueueUsingStacks: def __init__(self): self.stack_in = [] self.stack_out = [] def enqueue(self, x): self.stack_in.append(x) def dequeue(self): if not self.stack_out: while self.stack_in: self.stack_out.append(self.stack_in.pop()) if self.stack_out: return self.stack_out.pop() return None q = QueueUsingStacks() q.enqueue(5) q.enqueue(10) q.enqueue(15) q.dequeue() q.enqueue(20) q.dequeue()
Attempts:
2 left
💡 Hint
Trace each enqueue and dequeue step carefully.
✗ Incorrect
After first dequeue, stack_out has [10, 5] (top is 5 popped). After second dequeue, 10 is popped from stack_out leaving [15]. stack_in has 20 from last enqueue.