Bird
Raised Fist0
DSA Rustprogramming~10 mins

Sliding Window on Arrays in DSA Rust - Interactive Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to initialize the window sum with the first k elements.

DSA Rust
let window_sum: i32 = nums.iter().take([1]).copied().sum();
Drag options to blanks, or click blank then click option'
Ak
Bnums.len()
C0
D1
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Using nums.len() instead of k causes summing the whole array.
Using 0 or 1 as the count leads to incorrect window size.
2fill in blank
medium

Complete the code to slide the window by removing the left element and adding the right element.

DSA Rust
window_sum = window_sum - nums[i - [1]] + nums[i];
Drag options to blanks, or click blank then click option'
Ai
B1
Ck
D0
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Subtracting nums[i - 1] instead of nums[i - k] shifts the window incorrectly.
Using i or 0 instead of k causes wrong index calculation.
3fill in blank
hard

Fix the error in the loop condition to correctly iterate over the sliding windows.

DSA Rust
for i in [1]..nums.len() {
Drag options to blanks, or click blank then click option'
Ak
B0
C1
Dnums.len() - k
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Starting at 0 causes index underflow when subtracting k.
Starting at 1 or nums.len() - k misses windows or causes errors.
4fill in blank
hard

Fill both blanks to create a vector of maximum sums for each sliding window.

DSA Rust
let mut max_sums = vec![nums.iter().take([1]).copied().sum()];
for i in [2]..nums.len() {
    // sliding window update
}
Drag options to blanks, or click blank then click option'
Ak
C1
D0
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Using 0 or 1 instead of k causes incorrect initialization or loop start.
Mixing different values for the two blanks breaks the logic.
5fill in blank
hard

Fill both blanks to update the window sum, track the max sum, and push it to the result vector.

DSA Rust
window_sum = window_sum - nums[i - [1]] + nums[i];
max_sum = max_sum.max(window_sum);
max_sums.push([2]);
Drag options to blanks, or click blank then click option'
Ak
Bmax_sum
Cwindow_sum
Di
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Pushing window_sum instead of max_sum causes incorrect results.
Using wrong index subtraction causes runtime errors.

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