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 elements to the stack?
Consider a stack with a maximum size of 3. We push elements 10, 20, and 30 in order. What is the printed state of the stack after these operations?
DSA Rust
use std::vec::Vec;
struct Stack {
data: Vec<i32>,
max_size: usize,
}
impl Stack {
fn new(size: usize) -> Self {
Stack { data: Vec::new(), max_size: size }
}
fn push(&mut self, value: i32) -> bool {
if self.data.len() < self.max_size {
self.data.push(value);
true
} else {
false
}
}
fn print(&self) {
for val in &self.data {
print!("{} -> ", val);
}
println!("null");
}
}
fn main() {
let mut stack = Stack::new(3);
stack.push(10);
stack.push(20);
stack.push(30);
stack.print();
}Attempts:
2 left
💡 Hint
Remember that elements are pushed in order and printed in the order they are stored in the vector.
✗ Incorrect
The stack stores elements in a vector. Pushing 10, then 20, then 30 adds them in that order. Printing iterates from the first to last element, so output is 10 -> 20 -> 30 -> null.
❓ Predict Output
intermediateWhat does the is_empty method return after popping all elements?
Given a stack with max size 2, we push 5 and 15, then pop twice. What is the output of is_empty() after these operations?
DSA Rust
use std::vec::Vec;
struct Stack {
data: Vec<i32>,
max_size: usize,
}
impl Stack {
fn new(size: usize) -> Self {
Stack { data: Vec::new(), max_size: size }
}
fn push(&mut self, value: i32) -> bool {
if self.data.len() < self.max_size {
self.data.push(value);
true
} else {
false
}
}
fn pop(&mut self) -> Option<i32> {
self.data.pop()
}
fn is_empty(&self) -> bool {
self.data.is_empty()
}
}
fn main() {
let mut stack = Stack::new(2);
stack.push(5);
stack.push(15);
stack.pop();
stack.pop();
println!("{}", stack.is_empty());
}Attempts:
2 left
💡 Hint
Check if the stack has any elements left after popping twice.
✗ Incorrect
After pushing two elements and popping twice, the stack is empty. The is_empty method returns true when no elements remain.
❓ Predict Output
advancedWhat is the output when pushing beyond stack capacity?
A stack has max size 2. We push 1, 2, then try to push 3. What is the output of the push method calls?
DSA Rust
use std::vec::Vec;
struct Stack {
data: Vec<i32>,
max_size: usize,
}
impl Stack {
fn new(size: usize) -> Self {
Stack { data: Vec::new(), max_size: size }
}
fn push(&mut self, value: i32) -> bool {
if self.data.len() < self.max_size {
self.data.push(value);
true
} else {
false
}
}
}
fn main() {
let mut stack = Stack::new(2);
println!("{}", stack.push(1));
println!("{}", stack.push(2));
println!("{}", stack.push(3));
}Attempts:
2 left
💡 Hint
The push method returns false if the stack is full.
✗ Incorrect
The first two pushes succeed because the stack is not full. The third push fails because the stack reached max size 2.
❓ Predict Output
advancedWhat error occurs when popping from an empty stack?
What happens if we pop from an empty stack in this Rust code?
DSA Rust
use std::vec::Vec;
struct Stack {
data: Vec<i32>,
}
impl Stack {
fn new() -> Self {
Stack { data: Vec::new() }
}
fn pop(&mut self) -> i32 {
self.data.pop().unwrap()
}
}
fn main() {
let mut stack = Stack::new();
stack.pop();
}Attempts:
2 left
💡 Hint
unwrap() on None causes a runtime panic.
✗ Incorrect
Calling unwrap() on None causes the program to panic with a message about unwrap on None value.
🧠 Conceptual
expertWhich condition correctly checks if the stack is full?
Given a stack struct with a vector data and max_size, which condition correctly checks if the stack is full?
Attempts:
2 left
💡 Hint
Full means the number of elements equals max size.
✗ Incorrect
The stack is full when the number of elements equals max_size. Less means not full, greater should not happen.
