Challenge - 5 Problems
Stack Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateWhat is the output after pushing and popping elements?
Consider a stack implemented using a linked list. The following operations are performed in order:
push(10), push(20), push(30), pop(), push(40), pop()
What is the printed state of the stack after these operations?
push(10), push(20), push(30), pop(), push(40), pop()
What is the printed state of the stack after these operations?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct Stack {
top: Option<Box<Node>>,
}
impl Stack {
fn new() -> Self {
Stack { top: None }
}
fn push(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: self.top.take(),
});
self.top = Some(new_node);
}
fn pop(&mut self) -> Option<i32> {
self.top.take().map(|node| {
self.top = node.next;
node.data
})
}
fn print_stack(&self) {
let mut current = &self.top;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
print!("null\n");
}
}
fn main() {
let mut stack = Stack::new();
stack.push(10);
stack.push(20);
stack.push(30);
stack.pop();
stack.push(40);
stack.pop();
stack.print_stack();
}Attempts:
2 left
💡 Hint
Remember that pop removes the top element and push adds a new top.
✗ Incorrect
After pushing 10, 20, 30, the stack is 30 -> 20 -> 10 -> null. Pop removes 30, stack becomes 20 -> 10 -> null. Push 40 adds 40 on top: 40 -> 20 -> 10 -> null. Pop removes 40, stack returns to 20 -> 10 -> null.
🧠 Conceptual
intermediateWhat is the time complexity of push and pop operations in a linked list stack?
In a stack implemented using a linked list, what is the time complexity of the push and pop operations?
Attempts:
2 left
💡 Hint
Think about how many nodes you need to traverse for each operation.
✗ Incorrect
Both push and pop operate only at the top of the stack, which is the head of the linked list. They do not require traversal, so both are O(1).
🔧 Debug
advancedIdentify the error in this pop method implementation
The following Rust code attempts to implement the pop method for a linked list stack. What error will it cause when compiled or run?
DSA Rust
fn pop(&mut self) -> Option<i32> {
if self.top.is_none() {
return None;
}
let node = self.top.unwrap();
self.top = node.next;
Some(node.data)
}Attempts:
2 left
💡 Hint
Consider ownership rules when using unwrap on Option inside a method with &mut self.
✗ Incorrect
Calling unwrap() on self.top moves the Box out of the Option, but self.top is borrowed mutably, so Rust forbids moving out of borrowed content. This causes a compile error.
📝 Syntax
advancedWhich option correctly implements push method for linked list stack in Rust?
Select the correct Rust code snippet that implements the push method for a linked list stack.
Attempts:
2 left
💡 Hint
Remember to take ownership of the current top node before linking it.
✗ Incorrect
Option A correctly uses self.top.take() to move the current top out safely and then assigns the new node as the new top wrapped in Some(Box::new(...)). Option A tries to use self.top directly without take(), causing ownership errors. Option A assigns a Node directly to self.top which expects Option>. Option A uses self.top without take(), causing ownership errors.
🚀 Application
expertWhat is the stack state after a sequence of mixed operations?
Given a linked list stack initially empty, the following operations are performed:
push(5), push(15), pop(), push(25), push(35), pop(), pop(), push(45)
What is the printed state of the stack after these operations?
push(5), push(15), pop(), push(25), push(35), pop(), pop(), push(45)
What is the printed state of the stack after these operations?
DSA Rust
struct Node {
data: i32,
next: Option<Box<Node>>,
}
struct Stack {
top: Option<Box<Node>>,
}
impl Stack {
fn new() -> Self {
Stack { top: None }
}
fn push(&mut self, data: i32) {
let new_node = Box::new(Node {
data,
next: self.top.take(),
});
self.top = Some(new_node);
}
fn pop(&mut self) -> Option<i32> {
self.top.take().map(|node| {
self.top = node.next;
node.data
})
}
fn print_stack(&self) {
let mut current = &self.top;
while let Some(node) = current {
print!("{} -> ", node.data);
current = &node.next;
}
print!("null\n");
}
}
fn main() {
let mut stack = Stack::new();
stack.push(5);
stack.push(15);
stack.pop();
stack.push(25);
stack.push(35);
stack.pop();
stack.pop();
stack.push(45);
stack.print_stack();
}Attempts:
2 left
💡 Hint
Track each push and pop carefully, updating the top of the stack.
✗ Incorrect
Operations step-by-step:
push(5): 5 -> null
push(15): 15 -> 5 -> null
pop(): removes 15, stack is 5 -> null
push(25): 25 -> 5 -> null
push(35): 35 -> 25 -> 5 -> null
pop(): removes 35, stack is 25 -> 5 -> null
pop(): removes 25, stack is 5 -> null
push(45): 45 -> 5 -> null
push(5): 5 -> null
push(15): 15 -> 5 -> null
pop(): removes 15, stack is 5 -> null
push(25): 25 -> 5 -> null
push(35): 35 -> 25 -> 5 -> null
pop(): removes 35, stack is 25 -> 5 -> null
pop(): removes 25, stack is 5 -> null
push(45): 45 -> 5 -> null
