After all operations, the deque contains [10, 30].
Final Answer:
[10, 30] -> Option A
Quick Check:
After pop_front, deque = [10, 30] [OK]
Hint: Track push/pop order carefully front and back [OK]
Common Mistakes:
Forgetting pop_front removes front element
Mixing order of push_front and push_back
Assuming pop_front removes back element
4. Identify the error in the following Rust code using VecDeque:
use std::collections::VecDeque;
fn main() {
let mut dq = VecDeque::new();
dq.push_front(5);
dq.push_back(10);
let x = dq.pop_back();
println!("{}", x);
}
medium
A. pop_back() does not return a value
B. Cannot print Option<i32> directly with {}
C. VecDeque does not have push_front method
D. dq must be immutable to pop elements
Solution
Step 1: Understand pop_back return type
pop_back() returns an Option containing the popped value or None if empty.
Step 2: Check println! usage
Using {} tries to print the Option<i32> directly, which causes a type mismatch error. {:?} or unwrap() is needed.
Final Answer:
Cannot print Option<i32> directly with {} -> Option B
Quick Check:
Option<i32> needs {:?} or unwrap() to print [OK]
Hint: Use {:?} or unwrap() to print Option values [OK]
Common Mistakes:
Trying to print Option with {} instead of {:?}
Assuming pop_back returns value directly, not Option
Thinking VecDeque lacks push_front method
5. You want to implement a sliding window maximum using a VecDeque in Rust. Which approach correctly maintains the deque to always have the maximum at the front while sliding the window?
hard
A. Remove elements from front smaller than new element before pushing it to front
B. Always push new element to front without removing any elements
C. Remove elements from back smaller than new element before pushing it to back
D. Remove elements from back larger than new element before pushing it to back
Solution
Step 1: Understand sliding window max logic with deque
To keep max at front, remove all smaller elements from back before adding new element at back.
Step 2: Analyze options for correct deque maintenance
Remove elements from back smaller than new element before pushing it to back correctly describes removing smaller elements from back before pushing new element to back. Others do not maintain max at front properly.
Final Answer:
Remove elements from back smaller than new element before pushing it to back -> Option C
Quick Check:
Sliding window max uses back removals of smaller elements [OK]
Hint: Pop smaller from back before push_back new max candidate [OK]
Common Mistakes:
Removing from front instead of back
Pushing new elements to front incorrectly
Removing larger elements instead of smaller
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.