Challenge - 5 Problems
Queue vs Stack Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output of this Rust code using a stack?
Consider this Rust code that uses a stack to store numbers. What will be printed after all operations?
DSA Rust
fn main() {
let mut stack = Vec::new();
stack.push(10);
stack.push(20);
stack.push(30);
stack.pop();
stack.push(40);
while let Some(value) = stack.pop() {
print!("{} ", value);
}
}Attempts:
2 left
💡 Hint
Remember that a stack follows Last In First Out (LIFO) order.
✗ Incorrect
The stack pushes 10, 20, 30. Then it pops 30 (removes it). Then pushes 40. The stack now has 10, 20, 40. Popping all prints 40, then 20, then 10.
❓ Predict Output
intermediateWhat is the output of this Rust code using a queue?
Look at this Rust code that uses a queue to store numbers. What will be printed after all operations?
DSA Rust
use std::collections::VecDeque;
fn main() {
let mut queue = VecDeque::new();
queue.push_back(5);
queue.push_back(10);
queue.push_back(15);
queue.pop_front();
queue.push_back(20);
while let Some(value) = queue.pop_front() {
print!("{} ", value);
}
}Attempts:
2 left
💡 Hint
Remember that a queue follows First In First Out (FIFO) order.
✗ Incorrect
The queue adds 5, 10, 15. Then removes 5 from front. Then adds 20. The queue now has 10, 15, 20. Removing all prints 10, 15, 20.
🧠 Conceptual
advancedWhen is a stack preferred over a queue?
Which scenario best explains when to use a stack instead of a queue?
Attempts:
2 left
💡 Hint
Think about how undo works in apps.
✗ Incorrect
A stack is used when you want to reverse the order, such as undo actions where the last action done is the first to be undone.
🧠 Conceptual
advancedWhen is a queue preferred over a stack?
Which scenario best explains when to use a queue instead of a stack?
Attempts:
2 left
💡 Hint
Think about waiting lines or call centers.
✗ Incorrect
A queue is used when you want to process items in the order they arrive, like customers waiting in line or calls in a call center.
🔧 Debug
expertWhat error does this Rust code produce when using a stack incorrectly?
This Rust code tries to pop from an empty stack. What error will it cause?
DSA Rust
fn main() {
let mut stack: Vec<i32> = Vec::new();
stack.pop();
stack.pop();
}Attempts:
2 left
💡 Hint
Check what pop returns when the vector is empty.
✗ Incorrect
In Rust, pop returns an Option type. If the vector is empty, it returns None without panicking or error.
