Bird
Raised Fist0
DSA Rustprogramming~10 mins

Sliding Window Maximum Using Deque 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 create a new empty deque.

DSA Rust
let mut deq: std::collections::VecDeque<usize> = std::collections::VecDeque::[1]();
Drag options to blanks, or click blank then click option'
Adefault
Bwith_capacity
Cnew
Dfrom
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Using `with_capacity` requires a size argument.
Using `from` expects an existing collection.
Using `default` is valid but less explicit here.
2fill in blank
medium

Complete the code to remove indices from the back of the deque while the current number is greater.

DSA Rust
while let Some(&back) = deq.back() {
    if nums[back] < nums[i] {
        deq.[1]();
    } else {
        break;
    }
}
Drag options to blanks, or click blank then click option'
Apop_front
Bpush_front
Cpush_back
Dpop_back
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Using `pop_front()` removes from the front, which is incorrect here.
Using `push_back()` or `push_front()` adds elements instead of removing.
3fill in blank
hard

Fix the error in removing indices from the front of the deque when they are out of the current window.

DSA Rust
if let Some(&front) = deq.front() {
    if i [1] front + k {
        deq.pop_front();
    }
}
Drag options to blanks, or click blank then click option'
A>
B>=
C<
D-
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Using `<` or `>` instead of `>=` causes incorrect removals.
Not comparing with `i - k` but with `i + k`.
4fill in blank
hard

Fill both blanks to push the current index and add the maximum to the result vector.

DSA Rust
deq.[1](i);
if i + 1 >= k {
    result.push(nums[*deq.[2]().unwrap()].clone());
}
Drag options to blanks, or click blank then click option'
Apush_back
Bpop_front
Cfront
Dback
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Using `pop_front` instead of `front` to access the max index.
Using `back` to get max index, which is incorrect.
5fill in blank
hard

Fill all three blanks to complete the sliding window maximum function using a deque.

DSA Rust
pub fn max_sliding_window(nums: Vec<i32>, k: usize) -> Vec<i32> {
    use std::collections::VecDeque;
    let mut deq: VecDeque<usize> = VecDeque::[1]();
    let mut result = Vec::new();
    for i in 0..nums.len() {
        while let Some(&back) = deq.back() {
            if nums[back] < nums[i] {
                deq.[2]();
            } else {
                break;
            }
        }
        deq.push_back(i);
        if let Some(&front) = deq.front() {
            if front + k <= i {
                deq.[3]();
            }
        }
        if i + 1 >= k {
            result.push(nums[*deq.front().unwrap()]);
        }
    }
    result
}
Drag options to blanks, or click blank then click option'
Anew
Bpop_back
Cpop_front
Dpush_back
Attempts:
3 left
šŸ’” Hint
Common Mistakes
Using `push_back` instead of `pop_back` to remove elements.
Using `pop_front` incorrectly on the back side.
Not initializing the deque properly.

Practice

(1/5)
1. For nums = [1, 3, -1, -3, 5, 3, 6, 7] and k=3, what is the maximum of the first window [1,3,-1]?
easy
A. 1
B. -1
C. 3
D. 5

Solution

  1. Step 1: First window

    Window [1, 3, -1] contains elements at indices 0, 1, 2.
  2. Step 2: Maximum

    max(1, 3, -1) = 3.
  3. Step 3: Deque state

    After processing first window: deque = [1] (index of 3, since 3 is largest and -1 is removed because 3 > -1 and 3 came before -1).
  4. Final Answer:

    3 -> Option C
  5. Quick Check:

    max(1,3,-1)=3. [OK]
Hint: max(1,3,-1)=3. Deque front holds index 1 (value 3) after processing window. [OK]
Common Mistakes:
  • Returning the last element -1
  • Returning the first element 1
  • Not recognizing 3 as the maximum
2. In the sliding window maximum problem, what does the deque store?
medium
A. The maximum values seen so far
B. Indices of elements in decreasing order of their values
C. All elements in the current window
D. The sum of the current window

Solution

  1. Step 1: Deque content

    The deque stores INDICES (not values), arranged so that nums[deque.front()] is always the maximum of the current window.
  2. Step 2: Decreasing order

    Elements are kept in decreasing order of their values - smaller elements behind a larger one are useless (they can never be the maximum while the larger element is in the window).
  3. Final Answer:

    Indices of elements in decreasing value order. -> Option B
  4. Quick Check:

    Deque = indices in decreasing value order. Front = current window max. [OK]
Hint: Deque stores indices, decreasing by value. Front index = current max. [OK]
Common Mistakes:
  • Thinking deque stores values not indices
  • Thinking all window elements are stored
  • Confusing with a max-heap approach
3. What is the time complexity of sliding window maximum using a monotonic deque?
medium
A. O(n²)
B. O(n log n)
C. O(n)
D. O(n Ɨ k)

Solution

  1. Step 1: Each element pushed once

    Each element is pushed to the deque exactly once.
  2. Step 2: Each element popped at most once

    Each element is popped from either front or back at most once. Total push+pop operations: 2n = O(n).
  3. Step 3: Overall

    n iterations Ɨ O(1) amortized per iteration = O(n).
  4. Final Answer:

    O(n) - each element pushed and popped at most once. -> Option C
  5. Quick Check:

    Each element: 1 push + at most 1 pop = O(n) total. [OK]
Hint: Monotonic deque: each element pushed once, popped once = O(n). Better than O(nƗk) naive. [OK]
Common Mistakes:
  • Saying O(nƗk) by not accounting for amortized analysis
  • Saying O(n log n) confusing with heap-based approach
  • Not knowing each element is pushed/popped at most once total
4. When processing a new element i in sliding window maximum, when do we pop from the BACK of the deque?
hard
A. When deque.back() index is outside the window
B. When nums[deque.back()] <= nums[i] - smaller elements are useless
C. When the deque is full (size == k)
D. When nums[i] is negative

Solution

  1. Step 1: Why pop from back

    If nums[deque.back()] <= nums[i], the back element can never be the window maximum while nums[i] is in the window. nums[i] is larger AND will leave the window later.
  2. Step 2: Keep decreasing order

    Pop all elements from back where nums[back] <= nums[i], then push i. This maintains the monotonic decreasing invariant.
  3. Final Answer:

    Pop back when nums[deque.back()] <= nums[i]. -> Option B
  4. Quick Check:

    Back pop: current element >= back element → back is useless, remove it. [OK]
Hint: Pop back when nums[back] <= nums[current]. Larger current makes smaller back useless forever. [OK]
Common Mistakes:
  • Confusing front pop (out-of-window check) with back pop (useless element removal)
  • Popping only when strictly less than, missing equals case
  • Thinking deque size triggers back pop
5. When processing a new element i in sliding window maximum, when do we pop from the FRONT of the deque?
hard
A. When nums[deque.front()] < nums[i]
B. When deque.front() <= i - k (front index is outside the current window)
C. When the deque has more than k elements
D. When nums[i] is the new maximum

Solution

  1. Step 1: Window boundary

    The current window is [i-k+1, i]. The front of deque holds the maximum's index. If that index <= i-k, it's outside the window.
  2. Step 2: Pop expired maximum

    Pop front when deque.front() <= i - k. The front is no longer in the current window - the next front becomes the new maximum.
  3. Final Answer:

    Pop front when front index <= i - k (out of window). -> Option B
  4. Quick Check:

    Front pop: index out of window (front <= i-k). Back pop: value useless (back <= current). [OK]
Hint: Front pop = expired (index out of window). Back pop = useless (value smaller than current). [OK]
Common Mistakes:
  • Confusing front pop condition with back pop condition
  • Using < i-k instead of <= i-k (off by one)
  • Thinking front pop is based on value comparison
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