Challenge - 5 Problems
Dequeue Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Dequeue Operations
What is the printed state of the dequeue after these operations?
Operations:
1. Add 10 to front
2. Add 20 to rear
3. Add 30 to front
4. Remove from rear
5. Add 40 to rear
Operations:
1. Add 10 to front
2. Add 20 to rear
3. Add 30 to front
4. Remove from rear
5. Add 40 to rear
DSA Rust
struct Node {
value: i32,
next: Option<Box<Node>>,
prev: Option<*mut Node>,
}
struct Dequeue {
front: Option<Box<Node>>,
rear: Option<*mut Node>,
}
impl Dequeue {
fn new() -> Self {
Dequeue { front: None, rear: None }
}
fn add_front(&mut self, val: i32) {
let mut new_node = Box::new(Node { value: val, next: self.front.take(), prev: None });
let raw_new: *mut _ = &mut *new_node;
if let Some(old_front) = &mut new_node.next {
old_front.prev = Some(raw_new);
} else {
self.rear = Some(raw_new);
}
self.front = Some(new_node);
}
fn add_rear(&mut self, val: i32) {
let mut new_node = Box::new(Node { value: val, next: None, prev: self.rear });
let raw_new: *mut _ = &mut *new_node;
unsafe {
if let Some(rear_ptr) = self.rear {
(*rear_ptr).next = Some(new_node);
} else {
self.front = Some(new_node);
}
}
self.rear = Some(raw_new);
}
fn remove_rear(&mut self) -> Option<i32> {
unsafe {
self.rear.map(|rear_ptr| {
let val = (*rear_ptr).value;
if let Some(prev_ptr) = (*rear_ptr).prev {
(*prev_ptr).next = None;
self.rear = Some(prev_ptr);
} else {
self.front = None;
self.rear = None;
}
val
})
}
}
fn print_dequeue(&self) {
let mut current = &self.front;
let mut result = vec![];
while let Some(node) = current {
result.push(node.value);
current = &node.next;
}
println!("{:?}", result);
}
}
fn main() {
let mut dq = Dequeue::new();
dq.add_front(10);
dq.add_rear(20);
dq.add_front(30);
dq.remove_rear();
dq.add_rear(40);
dq.print_dequeue();
}Attempts:
2 left
💡 Hint
Trace each operation step by step, updating the front and rear pointers.
✗ Incorrect
After adding 10 front: [10]. Add 20 rear: [10, 20]. Add 30 front: [30, 10, 20]. Remove rear removes 20: [30, 10]. Add 40 rear: [30, 10, 40].
🧠 Conceptual
intermediateUnderstanding Dequeue Node Links
In a doubly linked list used for a dequeue, what does the 'prev' pointer of the front node point to?
Attempts:
2 left
💡 Hint
Think about the start of the list and what comes before it.
✗ Incorrect
The front node has no previous node, so its 'prev' pointer is None (null).
🔧 Debug
advancedIdentify the Bug in Remove Rear Method
What error will occur when running the remove_rear method if the dequeue has only one node?
DSA Rust
fn remove_rear(&mut self) -> Option<i32> {
unsafe {
self.rear.map(|rear_ptr| {
let val = (*rear_ptr).value;
if let Some(prev_ptr) = (*rear_ptr).prev {
(*prev_ptr).next = None;
self.rear = Some(prev_ptr);
} else {
self.front = None;
self.rear = None;
}
val
})
}
}Attempts:
2 left
💡 Hint
Check the condition when prev_ptr is None.
✗ Incorrect
When there is only one node, prev_ptr is None, so front and rear are set to None safely. No error occurs.
❓ Predict Output
advancedResult After Mixed Add and Remove Operations
What is the printed state of the dequeue after these operations?
Operations:
1. Add 5 to rear
2. Add 15 to rear
3. Add 25 to front
4. Remove rear
5. Remove front
6. Add 35 to front
Operations:
1. Add 5 to rear
2. Add 15 to rear
3. Add 25 to front
4. Remove rear
5. Remove front
6. Add 35 to front
DSA Rust
fn main() {
let mut dq = Dequeue::new();
dq.add_rear(5);
dq.add_rear(15);
dq.add_front(25);
dq.remove_rear();
dq.remove_front();
dq.add_front(35);
dq.print_dequeue();
}Attempts:
2 left
💡 Hint
Track the list after each operation carefully.
✗ Incorrect
Add rear 5: [5]. Add rear 15: [5, 15]. Add front 25: [25, 5, 15]. Remove rear removes 15: [25, 5]. Remove front removes 25: [5]. Add front 35: [35, 5].
🧠 Conceptual
expertWhy Use Doubly Linked List for Dequeue?
Why is a doubly linked list preferred over a singly linked list for implementing a dequeue?
Attempts:
2 left
💡 Hint
Think about operations at both front and rear ends.
✗ Incorrect
A doubly linked list has pointers to both next and previous nodes, enabling efficient insertions and deletions at both ends, which is essential for dequeue.
