The 'peek' operation returns the top element of the stack without removing it, allowing you to see the last added item.
Click to reveal answer
beginner
In Rust, which method is commonly used to peek the top element of a stack implemented with a Vec?
The 'last()' method is used to peek the top element of a stack implemented with a Vec, returning an Option with a reference to the last element.
Click to reveal answer
intermediate
Why does the peek operation return an Option type in Rust?
Because the stack might be empty, the peek operation returns an Option to safely handle the case where there is no top element (None) or some element (Some).
Click to reveal answer
beginner
What is the difference between 'pop' and 'peek' in a stack?
'Pop' removes and returns the top element, while 'peek' only returns the top element without removing it.
Click to reveal answer
intermediate
How would you handle an empty stack when peeking in Rust?
You check if the Option returned by 'last()' is None, which means the stack is empty, and handle that case accordingly to avoid errors.
Click to reveal answer
What does the 'peek' operation return when the stack is empty?
AAn error
BThe last element
CZero
DNone
✗ Incorrect
'peek' returns None wrapped in an Option when the stack is empty, indicating no element is available.
Which Rust Vec method is used to peek the top element of a stack?
Apop()
Blast()
Cfirst()
Dremove()
✗ Incorrect
'last()' returns a reference to the last element without removing it, perfect for peeking.
What type does 'last()' return when peeking a stack in Rust?
AOption<&T>
BResult<T, Error>
C&T
DT
✗ Incorrect
'last()' returns Option<&T> to safely handle empty stacks.
What is the main difference between 'peek' and 'pop' in stack operations?
ABoth remove the element
BPeek removes the element, pop does not
CPop removes the element, peek does not
DNeither remove the element
✗ Incorrect
'Pop' removes and returns the top element; 'peek' only returns it without removal.
If you want to safely check the top element of a stack in Rust, what should you do?
AUse last() and handle the Option result
BUse unwrap() on last()
CUse pop()
DUse first()
✗ Incorrect
You should use last() and handle the Option to avoid panics if the stack is empty.
Explain how to peek the top element of a stack implemented with Vec in Rust, including how to handle empty stacks.
Think about safe access and Option handling.
You got /4 concepts.
Describe the difference between peek and pop operations in a stack and why peek is useful.
Consider what happens to the stack after each operation.
You got /3 concepts.
Practice
(1/5)
1. What does the peek operation do in a stack data structure?
easy
A. Returns the top element without removing it
B. Removes the top element and returns it
C. Adds a new element to the top
D. Checks if the stack is empty
Solution
Step 1: Understand stack operations
A stack stores elements in a last-in, first-out order. Common operations include push, pop, and peek.
Step 2: Define peek operation
Peek returns the top element without removing it, allowing you to see the current top value safely.
Final Answer:
Returns the top element without removing it -> Option A
Quick Check:
Peek = top element without removal [OK]
Hint: Peek shows top without changing stack [OK]
Common Mistakes:
Confusing peek with pop operation
Thinking peek adds elements
Assuming peek checks emptiness
2. Which of the following Rust code snippets correctly peeks the top element of a stack implemented as a Vec<i32> named stack?
easy
A. let top = stack.last();
B. let top = stack[0];
C. let top = stack.pop();
D. let top = stack.first();
Solution
Step 1: Understand Rust Vec methods for stack
In Rust, Vec can be used as a stack. pop() removes and returns the last element, last() returns a reference to the last element without removing it.
Step 2: Identify correct peek method
Peek means to see the top element without removing it, so stack.last() is correct. stack.pop() removes it, stack[0] accesses the first element, and stack.first() returns the first element, not the top.
Final Answer:
let top = stack.last(); -> Option A
Quick Check:
Peek = stack.last() [OK]
Hint: Use .last() to peek top element in Rust Vec stack [OK]
Common Mistakes:
Using pop() which removes element
Accessing first() instead of last()
Using index 0 which is bottom of stack
3. What will be the output of the following Rust code?
let mut stack = vec![10, 20, 30];
if let Some(top) = stack.last() {
println!("{}", top);
} else {
println!("Stack is empty");
}
medium
A. 20
B. 30
C. 10
D. Stack is empty
Solution
Step 1: Understand stack contents and last()
The stack vector contains [10, 20, 30]. The last element is 30.
Step 2: Trace the if let statement
The stack.last() returns Some(&30), so the if block prints 30.
Final Answer:
30 -> Option B
Quick Check:
stack.last() = 30 [OK]
Hint: stack.last() returns top element reference [OK]
Common Mistakes:
Printing bottom element instead of top
Confusing last() with pop() output
Assuming stack is empty
4. Identify the error in the following Rust code that tries to peek the top element of a stack and fix it:
let stack = vec![1, 2, 3];
let top = stack.pop().unwrap();
println!("Top element: {}", top);
medium
A. No error, code works correctly
B. pop() removes element; use last() to peek instead
C. unwrap() causes panic; use expect() instead
D. stack should be mutable to use pop()
Solution
Step 1: Check mutability for pop()
pop() removes the last element, so the vector must be mutable. Here, stack is immutable.
Step 2: Fix by making stack mutable
Change let stack = vec![1, 2, 3]; to let mut stack = vec![1, 2, 3]; to allow pop().
Final Answer:
stack should be mutable to use pop() -> Option D
Quick Check:
pop() needs mutable stack [OK]
Hint: pop() requires mutable stack variable [OK]
Common Mistakes:
Ignoring mut keyword error
Confusing pop() with peek operation
Assuming unwrap() always safe
5. Given a stack implemented as Vec<i32>, write a Rust function peek_top that returns the top element as Option<i32> without removing it. What is the correct implementation?
hard
A. fn peek_top(stack: &mut Vec<i32>) -> Option<i32> { stack.pop() }
B. fn peek_top(stack: &Vec<i32>) -> i32 { *stack.last().unwrap() }
C. fn peek_top(stack: &Vec<i32>) -> Option<i32> { stack.last().copied() }
D. fn peek_top(stack: Vec<i32>) -> Option<i32> { stack.first().copied() }
Solution
Step 1: Understand function requirements
The function must peek without removing, so it should borrow stack immutably and return Option<i32>.
Step 2: Analyze options
fn peek_top(stack: &Vec<i32>) -> Option<i32> { stack.last().copied() } uses stack.last().copied() which returns Option<i32> by copying the reference. fn peek_top(stack: &mut Vec<i32>) -> Option<i32> { stack.pop() } removes element (pop), fn peek_top(stack: &Vec<i32>) -> i32 { *stack.last().unwrap() } unwraps and returns i32 (not Option), fn peek_top(stack: Vec<i32>) -> Option<i32> { stack.first().copied() } uses first() which is bottom element.