Challenge - 5 Problems
Two Stacks Queue Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of enqueue and dequeue operations
What is the printed state of the queue after these operations using two stacks?
DSA Rust
struct Queue {
stack_in: Vec<i32>,
stack_out: Vec<i32>,
}
impl Queue {
fn new() -> Self {
Queue { stack_in: Vec::new(), stack_out: Vec::new() }
}
fn enqueue(&mut self, val: i32) {
self.stack_in.push(val);
}
fn dequeue(&mut self) -> Option<i32> {
if self.stack_out.is_empty() {
while let Some(x) = self.stack_in.pop() {
self.stack_out.push(x);
}
}
self.stack_out.pop()
}
fn print_queue(&mut self) {
let mut temp = Vec::new();
while let Some(x) = self.dequeue() {
print!("{} -> ", x);
temp.push(x);
}
println!("null");
for x in temp.into_iter().rev() {
self.stack_out.push(x);
}
}
}
fn main() {
let mut q = Queue::new();
q.enqueue(10);
q.enqueue(20);
q.enqueue(30);
q.dequeue();
q.enqueue(40);
q.print_queue();
}Attempts:
2 left
💡 Hint
Remember dequeue removes the oldest element, and enqueue adds to the newest.
✗ Incorrect
The first dequeue removes 10. Then the queue has 20, 30, and after enqueue 40, the queue is 20 -> 30 -> 40 -> null.
🧠 Conceptual
intermediateWhy use two stacks to implement a queue?
What is the main reason for using two stacks to implement a queue?
Attempts:
2 left
💡 Hint
Think about how stacks and queues differ in order of removal.
✗ Incorrect
Two stacks reverse the order of elements twice, so the first inserted element comes out first, mimicking a queue's FIFO behavior.
🔧 Debug
advancedIdentify the error in dequeue method
What error will occur when running this dequeue method if stack_out is empty and stack_in is also empty?
DSA Rust
fn dequeue(&mut self) -> i32 {
if self.stack_out.is_empty() {
while let Some(x) = self.stack_in.pop() {
self.stack_out.push(x);
}
}
self.stack_out.pop().unwrap()
}Attempts:
2 left
💡 Hint
What happens if you unwrap an Option that is None?
✗ Incorrect
Calling unwrap on None causes a panic at runtime if both stacks are empty and no element to pop.
❓ Predict Output
advancedFinal state of stacks after operations
After these operations, what is the content of stack_in and stack_out?
DSA Rust
let mut q = Queue::new(); q.enqueue(1); q.enqueue(2); q.enqueue(3); q.dequeue(); q.enqueue(4); q.dequeue();
Attempts:
2 left
💡 Hint
Trace each enqueue and dequeue carefully, noting when elements move between stacks.
✗ Incorrect
After enqueuing 1,2,3, first dequeue moves to stack_out [3,2,1] (top 1), pop 1 leaves [3,2] (top 2). Enqueue 4: stack_in [4]. Second dequeue pops 2 leaving stack_out [3], stack_in [4].
🚀 Application
expertTime complexity of dequeue operation in two-stack queue
What is the amortized 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_out), so amortized dequeue is O(1).
