Challenge - 5 Problems
Dynamic Stack Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Push and Pop Operations on Dynamic Stack
What is the printed state of the stack after the following operations?
Push 10, Push 20, Pop, Push 30, Push 40, Pop
Push 10, Push 20, Pop, Push 30, Push 40, Pop
DSA Rust
struct Stack {
data: Vec<i32>,
}
impl Stack {
fn new() -> Self {
Stack { data: Vec::new() }
}
fn push(&mut self, val: i32) {
self.data.push(val);
}
fn pop(&mut self) -> Option<i32> {
self.data.pop()
}
fn print(&self) {
for val in self.data.iter() {
print!("{} -> ", val);
}
println!("null");
}
}
fn main() {
let mut stack = Stack::new();
stack.push(10);
stack.push(20);
stack.pop();
stack.push(30);
stack.push(40);
stack.pop();
stack.print();
}Attempts:
2 left
💡 Hint
Remember that pop removes the last pushed element.
✗ Incorrect
After pushing 10 and 20, pop removes 20. Then push 30 and 40, pop removes 40. Remaining elements are 10 and 30.
🧠 Conceptual
intermediateCapacity Growth in Dynamic Stack
If a dynamic stack implemented with a resizable array doubles its capacity when full, what is the capacity after pushing 9 elements starting from an initial capacity of 4?
Attempts:
2 left
💡 Hint
Capacity doubles only when full.
✗ Incorrect
Starting capacity 4, after pushing 4 elements capacity doubles to 8. When pushing the 9th element, capacity doubles again to 16.
🔧 Debug
advancedIdentify the Bug in Pop Method
What error occurs when running this pop method if the stack is empty?
fn pop(&mut self) -> i32 { self.data.pop().unwrap() }
fn pop(&mut self) -> i32 { self.data.pop().unwrap() }
DSA Rust
struct Stack {
data: Vec<i32>,
}
impl Stack {
fn pop(&mut self) -> i32 {
self.data.pop().unwrap()
}
}
fn main() {
let mut stack = Stack { data: Vec::new() };
stack.pop();
}Attempts:
2 left
💡 Hint
What happens when unwrap is called on None?
✗ Incorrect
Calling unwrap on None causes a runtime panic with the message shown in option C.
📝 Syntax
advancedCorrect Syntax for Push Method
Which option correctly implements the push method for the dynamic stack?
DSA Rust
struct Stack {
data: Vec<i32>,
}
impl Stack {
fn push(&mut self, val: i32) {
// fill here
}
}Attempts:
2 left
💡 Hint
Vec uses push method to add elements.
✗ Incorrect
Vec has a push method. self.data.push(val) correctly adds val to the vector.
🚀 Application
expertFinal Stack State After Mixed Operations
Given a dynamic stack starting empty, what is the final printed stack after these operations?
Push 5, Push 10, Push 15, Pop, Push 20, Pop, Pop, Push 25, Push 30
Push 5, Push 10, Push 15, Pop, Push 20, Pop, Pop, Push 25, Push 30
DSA Rust
struct Stack {
data: Vec<i32>,
}
impl Stack {
fn new() -> Self {
Stack { data: Vec::new() }
}
fn push(&mut self, val: i32) {
self.data.push(val);
}
fn pop(&mut self) -> Option<i32> {
self.data.pop()
}
fn print(&self) {
for val in self.data.iter() {
print!("{} -> ", val);
}
println!("null");
}
}
fn main() {
let mut stack = Stack::new();
stack.push(5);
stack.push(10);
stack.push(15);
stack.pop();
stack.push(20);
stack.pop();
stack.pop();
stack.push(25);
stack.push(30);
stack.print();
}Attempts:
2 left
💡 Hint
Track each push and pop carefully in order.
✗ Incorrect
Push 5,10,15 stack: 5,10,15; pop removes 15; push 20 stack: 5,10,20; pop removes 20; pop removes 10; push 25,30 stack: 5,25,30.
