Challenge - 5 Problems
Sliding Window Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of Sliding Window Maximum for a Simple Array
What is the output of the sliding window maximum for the array [1, 3, -1, -3, 5, 3, 6, 7] with window size 3 using a deque?
DSA Rust
fn sliding_window_maximum(nums: Vec<i32>, k: usize) -> Vec<i32> {
use std::collections::VecDeque;
let mut deq: VecDeque<usize> = VecDeque::new();
let mut result = Vec::new();
for i in 0..nums.len() {
while let Some(&back) = deq.back() {
if nums[back] < nums[i] {
deq.pop_back();
} else {
break;
}
}
deq.push_back(i);
if let Some(&front) = deq.front() {
if front + k <= i {
deq.pop_front();
}
}
if i + 1 >= k {
result.push(nums[*deq.front().unwrap()]);
}
}
result
}
fn main() {
let nums = vec![1, 3, -1, -3, 5, 3, 6, 7];
let k = 3;
let output = sliding_window_maximum(nums, k);
println!("{:?}", output);
}Attempts:
2 left
š” Hint
Remember the deque stores indices of elements in decreasing order of their values within the current window.
ā Incorrect
The sliding window maximum for each window of size 3 is computed by maintaining a deque of indices of elements in decreasing order. The maximum for each window is the element at the front of the deque. For the given array and window size, the maximums are [3, 3, 5, 5, 6, 7].
š§ Conceptual
intermediateUnderstanding Deque Behavior in Sliding Window Maximum
In the sliding window maximum algorithm using a deque, what is the main reason for removing elements from the back of the deque when a new element arrives?
Attempts:
2 left
š” Hint
Think about why smaller elements behind a bigger element are useless for maximum queries.
ā Incorrect
Elements smaller than the new element are removed from the back because they cannot be the maximum in the current or any future window that includes the new element. This keeps the deque in decreasing order of values, allowing quick access to the maximum.
š§ Debug
advancedIdentify the Bug in Sliding Window Maximum Implementation
Given the following Rust code snippet for sliding window maximum, what error or bug will it cause when run?
DSA Rust
fn sliding_window_maximum(nums: Vec<i32>, k: usize) -> Vec<i32> {
use std::collections::VecDeque;
let mut deq: VecDeque<usize> = VecDeque::new();
let mut result = Vec::new();
for i in 0..nums.len() {
while let Some(&back) = deq.back() {
if nums[back] <= nums[i] {
deq.pop_back();
} else {
break;
}
}
deq.push_back(i);
if let Some(&front) = deq.front() {
if front + k < i {
deq.pop_front();
}
}
if i + 1 >= k {
result.push(nums[*deq.front().unwrap()]);
}
}
result
}Attempts:
2 left
š” Hint
Check the condition that removes indices outside the current window.
ā Incorrect
The condition 'if front + k < i' should be 'if front + k <= i' to correctly remove indices that are out of the current window. Otherwise, outdated indices remain, causing incorrect maximum values.
ā Predict Output
advancedOutput of Sliding Window Maximum with All Equal Elements
What is the output of the sliding window maximum for the array [2, 2, 2, 2, 2] with window size 2 using a deque?
DSA Rust
fn sliding_window_maximum(nums: Vec<i32>, k: usize) -> Vec<i32> {
use std::collections::VecDeque;
let mut deq: VecDeque<usize> = VecDeque::new();
let mut result = Vec::new();
for i in 0..nums.len() {
while let Some(&back) = deq.back() {
if nums[back] < nums[i] {
deq.pop_back();
} else {
break;
}
}
deq.push_back(i);
if let Some(&front) = deq.front() {
if front + k <= i {
deq.pop_front();
}
}
if i + 1 >= k {
result.push(nums[*deq.front().unwrap()]);
}
}
result
}
fn main() {
let nums = vec![2, 2, 2, 2, 2];
let k = 2;
let output = sliding_window_maximum(nums, k);
println!("{:?}", output);
}Attempts:
2 left
š” Hint
All elements are equal, so maximum in each window is the same as the element value.
ā Incorrect
For window size 2, the number of windows is len(nums) - k + 1 = 4. Each window contains two 2's, so maximum is always 2. The output is [2, 2, 2, 2].
š§ Conceptual
expertTime Complexity of Sliding Window Maximum Using Deque
What is the time complexity of the sliding window maximum algorithm using a deque for an input array of size n and window size k?
Attempts:
2 left
š” Hint
Consider how many times each element is added and removed from the deque.
ā Incorrect
Each element is added to the deque once and removed once, so total operations are proportional to n. Hence, the time complexity is O(n).
