Bird
Raised Fist0
DSA Rustprogramming~20 mins

Sliding Window on Arrays in DSA Rust - Practice Problems & Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
šŸŽ–ļø
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
ā“ Predict Output
intermediate
2:00remaining
Output 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);
}
A[9, 15, 21]
B[1, 3, 5, 7, 9]
C[3, 5, 7]
D[16, 24]
Attempts:
2 left
šŸ’” Hint
Think about how many windows of size 3 fit in the array and what their sums are.
🧠 Conceptual
intermediate
1:30remaining
Sliding 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?
A3
B4
C1
D2
Attempts:
2 left
šŸ’” Hint
Check each window of size 3 and find its max, then count how many exceed 4.
šŸ”§ Debug
advanced
2:00remaining
Identify 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);
    }
}
APanics at runtime due to unwrap on None
BPrints 4, 2, 3, 12
CLogic error: misses the last window due to exclusive range
DPrints 4, 2, 3
Attempts:
2 left
šŸ’” Hint
Check the range used in the for loop and how many windows it covers.
šŸš€ Application
advanced
2:00remaining
Find 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?
A36
B35
C30
D39
Attempts:
2 left
šŸ’” Hint
Calculate sums of all windows of size 4 and find the largest.
ā“ Predict Output
expert
2:30remaining
Output 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);
}
A[5.0, 4.666666666666667, 5.666666666666667, 6.666666666666667, 9.666666666666666]
B[5.0, 6.0, 5.0, 6.0, 9.0]
C[5, 4, 5, 6, 9]
D[3.0, 5.0, 7.0, 2.0, 8.0]
Attempts:
2 left
šŸ’” Hint
Remember the sliding window sum updates by adding new element and removing old element.

Practice

(1/5)
1. What is the main purpose of the sliding window technique on arrays?
easy
A. To sort the array in ascending order
B. To efficiently process subarrays of fixed size without recomputing everything
C. To reverse the array elements
D. To find the maximum element in the entire array

Solution

  1. Step 1: Understand the sliding window concept

    The sliding window technique moves a fixed-size window over the array to process subarrays efficiently.
  2. Step 2: Identify the purpose

    It avoids recomputing sums or other values for each subarray by updating results incrementally.
  3. Final Answer:

    To efficiently process subarrays of fixed size without recomputing everything -> Option B
  4. Quick Check:

    Sliding window = efficient fixed-size subarray processing [OK]
Hint: Think of a moving window that updates results quickly [OK]
Common Mistakes:
  • Confusing sliding window with sorting
  • Thinking it reverses the array
  • Assuming it finds max of whole array only
2. Which Rust code snippet correctly initializes a sliding window of size 3 over a vector nums?
easy
A. for i in 0..nums.len() - 2 { let window = &nums[i..i+3]; }
B. for i in 0..nums.len() { let window = &nums[i..i+3]; }
C. for i in 0..nums.len() - 1 { let window = &nums[i..i+3]; }
D. for i in 0..nums.len() - 3 { let window = &nums[i..i+3]; }

Solution

  1. Step 1: Determine valid loop range for window size 3

    Window covers indices i, i+1, i+2, so i+3 must be ≤ nums.len(). Thus, i < nums.len() - 2.
  2. Step 2: Check each option's range

    for i in 0..nums.len() - 2 { let window = &nums[i..i+3]; } uses 0..nums.len()-2, which is correct. Others are off by one or more.
  3. Final Answer:

    for i in 0..nums.len() - 2 { let window = &nums[i..i+3]; } -> Option A
  4. Quick Check:

    Loop till len-2 for size 3 window [OK]
Hint: Window end index = start + size; loop till len - size + 1 [OK]
Common Mistakes:
  • Using len()-3 which is too small
  • Looping till len() causing out-of-bounds
  • Incorrect slice ranges causing panic
3. What is the output of this Rust code?
let nums = vec![1, 3, 5, 7, 9];
let k = 3;
let mut sums = Vec::new();
for i in 0..nums.len() - k + 1 {
    let window_sum: i32 = nums[i..i+k].iter().sum();
    sums.push(window_sum);
}
println!("{:?}", sums);
medium
A. [3, 5, 7]
B. [1, 3, 5]
C. [9, 15, 21]
D. [7, 9, 11]

Solution

  1. Step 1: Identify windows and sums

    Windows of size 3 are: [1,3,5], [3,5,7], [5,7,9]. Their sums are 9, 15, 21 respectively.
  2. Step 2: Confirm output vector

    The sums vector collects these sums and prints them as [9, 15, 21].
  3. Final Answer:

    [9, 15, 21] -> Option C
  4. Quick Check:

    Sum windows = [9, 15, 21] [OK]
Hint: Sum each 3-element slice, slide by one [OK]
Common Mistakes:
  • Summing wrong slices
  • Off-by-one in loop range
  • Confusing sums with elements
4. Find the error in this Rust code that tries to print max sum of sliding window size 2:
let nums = vec![4, 2, 1, 7];
let k = 2;
let mut max_sum = 0;
for i in 0..nums.len() - k {
    let sum: i32 = nums[i..i+k].iter().sum();
    if sum > max_sum {
        max_sum = sum;
    }
}
println!("{}", max_sum);
medium
A. Window size k should be 3 instead of 2
B. max_sum should be initialized to i32::MIN instead of 0
C. Sum calculation should use fold instead of iter().sum()
D. Loop range should be 0..nums.len() - k + 1 to include last window

Solution

  1. Step 1: Check loop range for window size 2

    Windows are nums[i..i+2], so i must go up to nums.len() - 2 inclusive, meaning 0..nums.len()-1 or 0..nums.len()-k+1.
  2. Step 2: Identify off-by-one error

    Current loop ends at nums.len()-k exclusive, missing last window. Correct range is 0..nums.len()-k+1.
  3. Final Answer:

    Loop range should be 0..nums.len() - k + 1 to include last window -> Option D
  4. Quick Check:

    Loop till len-k+1 for full windows [OK]
Hint: Loop till len - window size + 1 to cover all windows [OK]
Common Mistakes:
  • Using wrong loop range causing missed windows
  • Initializing max_sum to 0 causing wrong max if negatives
  • Unnecessary changes to sum calculation
5. Given an array nums and window size k, how can you find the maximum sum of any subarray of size k using sliding window in Rust efficiently?
hard
A. Calculate sum of first k elements, then slide window by adding next element and subtracting first element of previous window, updating max sum each time
B. Sort the array and sum the last k elements to get maximum sum
C. Use nested loops to sum every subarray of size k and track maximum sum
D. Reverse the array and sum first k elements for maximum sum

Solution

  1. Step 1: Compute initial window sum

    Sum the first k elements to get the starting window sum.
  2. Step 2: Slide window and update sums

    For each next position, add the new element and subtract the element leaving the window, updating max sum if needed.
  3. Step 3: Avoid recomputing sums fully

    This approach avoids nested loops and is efficient with O(n) time complexity.
  4. Final Answer:

    Calculate sum of first k elements, then slide window by adding next element and subtracting first element of previous window, updating max sum each time -> Option A
  5. Quick Check:

    Sliding window sum update = efficient max sum [OK]
Hint: Add new element, subtract old to update sum fast [OK]
Common Mistakes:
  • Using nested loops causing slow O(n²) time
  • Sorting array which changes element order
  • Reversing array which doesn't help sum
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard