Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to initialize the window sum with the first k elements.
DSA Rust
let window_sum: i32 = nums.iter().take([1]).copied().sum();
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.
ā Incorrect
We take the first k elements to initialize the window sum for the sliding window.
2fill in blank
mediumComplete 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];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.
ā Incorrect
We subtract the element leaving the window at index i-k and add the new element at index i.
3fill in blank
hardFix the error in the loop condition to correctly iterate over the sliding windows.
DSA Rust
for i in [1]..nums.len() {
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.
ā Incorrect
The loop starts from k to slide the window from the end of the first window to the last element.
4fill in blank
hardFill 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 }
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.
ā Incorrect
Initialize max_sums with the sum of the first k elements and start the loop from k to slide the window.
5fill in blank
hardFill 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]);
Attempts:
3 left
š” Hint
Common Mistakes
Pushing window_sum instead of max_sum causes incorrect results.
Using wrong index subtraction causes runtime errors.
ā Incorrect
Subtract the element leaving the window at i-k, update max_sum, and push max_sum to max_sums vector.
