Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā Predict Output
intermediateOutput of sliding window sum calculation
What is the output of this Rust code that calculates the sum of each sliding window of size 3 in the array?
DSA Rust
fn main() {
let arr = [1, 3, 5, 7, 9];
let k = 3;
let mut sums = Vec::new();
for i in 0..=arr.len() - k {
let window_sum: i32 = arr[i..i + k].iter().sum();
sums.push(window_sum);
}
println!("{:?}", sums);
}Attempts:
2 left
š” Hint
Think about how many windows of size 3 fit in the array and what their sums are.
ā Incorrect
The sliding windows of size 3 are [1,3,5], [3,5,7], and [5,7,9]. Their sums are 9, 15, and 21 respectively.
š§ Conceptual
intermediateSliding window maximum count
Given an array [2, 1, 3, 4, 6, 2, 5, 1] and window size 3, how many sliding windows have a maximum value greater than 4?
Attempts:
2 left
š” Hint
Check each window of size 3 and find its max, then count how many exceed 4.
ā Incorrect
The windows are [2,1,3], [1,3,4], [3,4,6], [4,6,2], [6,2,5], [2,5,1]. Max values are 3,4,6,6,6,5. Those greater than 4 are 4 windows.
š§ Debug
advancedIdentify the error in sliding window minimum code
What error does this Rust code produce when trying to find minimum in each sliding window of size 2?
DSA Rust
fn main() {
let arr = [4, 2, 12, 3];
let k = 2;
for i in 0..=arr.len() - k {
let window_min = arr[i..i + k].iter().min().unwrap();
println!("{}", window_min);
}
}Attempts:
2 left
š” Hint
Check the range used in the for loop and how many windows it covers.
ā Incorrect
The loop runs from 0 to arr.len() - k (which is 2), so indices 0 and 1 only. But the last window starting at index 2 is missed. Also, the range is exclusive at the end, so it misses the last window.
š Application
advancedFind the maximum sum of any sliding window of size 4
Given the array [1, 4, 2, 10, 23, 3, 1, 0, 20] and window size 4, what is the maximum sum of any sliding window?
Attempts:
2 left
š” Hint
Calculate sums of all windows of size 4 and find the largest.
ā Incorrect
Sliding windows and sums: [1,4,2,10]=17, [4,2,10,23]=39, [2,10,23,3]=38, [10,23,3,1]=37, [23,3,1,0]=27, [3,1,0,20]=24. Max is 39.
ā Predict Output
expertOutput of optimized sliding window average calculation
What is the output of this Rust code that calculates the average of each sliding window of size 3 using an optimized approach?
DSA Rust
fn main() {
let arr = [3, 5, 7, 2, 8, 10, 11];
let k = 3;
let mut result = Vec::new();
let mut window_sum: i32 = arr[0..k].iter().sum();
result.push(window_sum as f64 / k as f64);
for i in k..arr.len() {
window_sum += arr[i] - arr[i - k];
result.push(window_sum as f64 / k as f64);
}
println!("{:?}", result);
}Attempts:
2 left
š” Hint
Remember the sliding window sum updates by adding new element and removing old element.
ā Incorrect
Initial sum is 3+5+7=15, average 5.0. Then windows sums: 15-3+2=14 (avg 4.6667), 14-5+8=17 (avg 5.6667), 17-7+10=20 (avg 6.6667), 20-2+11=29 (avg 9.6667).
