Sliding Window Maximum Using Deque in DSA Java - Time & Space Complexity
We want to understand how the time needed to find the maximum in each sliding window grows as the input array gets bigger.
How does the algorithm handle larger arrays efficiently?
Analyze the time complexity of the following code snippet.
public int[] maxSlidingWindow(int[] nums, int k) {
Deque<Integer> dq = new LinkedList<>();
int[] result = new int[nums.length - k + 1];
for (int i = 0; i < nums.length; i++) {
while (!dq.isEmpty() && dq.peekFirst() <= i - k) dq.pollFirst();
while (!dq.isEmpty() && nums[dq.peekLast()] < nums[i]) dq.pollLast();
dq.offerLast(i);
if (i >= k - 1) result[i - k + 1] = nums[dq.peekFirst()];
}
return result;
}
This code finds the maximum value in every window of size k as it slides over the array.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: A single loop that goes through each element once.
- How many times: Each element is added and removed from the deque at most once.
As the input size grows, each element is processed a limited number of times, so the total work grows roughly in direct proportion to the input size.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 20 operations (each element added and removed once) |
| 100 | About 200 operations |
| 1000 | About 2000 operations |
Pattern observation: The operations grow linearly with input size, not faster.
Time Complexity: O(n)
This means the time to complete the task grows directly in proportion to the number of elements.
[X] Wrong: "Because there are nested while loops, the time complexity must be O(n²)."
[OK] Correct: Each element is added and removed from the deque only once, so the inner loops do not run for every element repeatedly.
Understanding this linear time solution shows you can use data structures smartly to avoid repeated work, a key skill in many coding challenges.
"What if we used a simple array instead of a deque to track maximums? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of deque
A deque stores indices of elements in a way that the front always holds the index of the maximum element for the current window.Step 2: Why use indices and not values
Storing indices allows easy removal of elements that are out of the current window range and helps maintain order efficiently.Final Answer:
To store indices of elements in a way that helps find the maximum in O(1) time per window -> Option DQuick Check:
Deque stores indices for max tracking [OK]
- Storing values instead of indices
- Using deque to sort the array
- Ignoring window boundaries when removing elements
Solution
Step 1: Identify correct deque initialization
In Java, Deque is an interface and LinkedList implements it. So, Deque<Integer> dq = new LinkedList<>(); is valid.Step 2: Why other options are invalid
Deque<int> dq = new ArrayDeque<>(); uses primitive int which is not allowed in generics. Deque dq = new ArrayList<>(); uses ArrayList which does not implement Deque. Deque<Integer> dq = new Stack<>(); uses Stack which is not a Deque.Final Answer:
Deque<Integer> dq = new LinkedList<>(); -> Option BQuick Check:
Use LinkedList for Deque in Java [OK]
- Using primitive types in generics
- Confusing ArrayList with Deque
- Using Stack instead of Deque
[1,3,-1,-3,5,3,6,7] with window size 3?Solution
Step 1: Slide window and find max for each window
Windows and max values: [1,3,-1] -> 3, [3,-1,-3] -> 3, [-1,-3,5] -> 5, [-3,5,3] -> 5, [5,3,6] -> 6, [3,6,7] -> 7.Step 2: Collect all max values in order
The output array is [3, 3, 5, 5, 6, 7].Final Answer:
[3, 3, 5, 5, 6, 7] -> Option AQuick Check:
Max per window = [3,3,5,5,6,7] [OK]
- Confusing window boundaries
- Taking max of wrong elements
- Off-by-one errors in indexing
Deque<Integer> dq = new LinkedList<>();
for (int i = 0; i < nums.length; i++) {
while (!dq.isEmpty() && nums[dq.peekLast()] < nums[i]) {
dq.pollLast();
}
dq.addLast(i);
if (dq.peekFirst() <= i - k) {
dq.pollFirst();
}
if (i >= k - 1) {
result[i - k + 1] = nums[dq.peekFirst()];
}
}Solution
Step 1: Check order of removing out-of-window indices
Indices out of the current window must be removed before adding the new index to maintain correct window elements.Step 2: Why current order causes error
Removing out-of-window indices after adding new index can cause incorrect max calculation because the front may be outdated.Final Answer:
The condition to remove indices out of window should be before adding new index -> Option CQuick Check:
Remove old indices before adding new [OK]
- Removing out-of-window indices too late
- Storing values instead of indices
- Ignoring initialization of result array
nums = [4, 2, 12, 3, 8, 7, 9] and window size 4, which of the following is the correct sliding window maximum output using a deque approach?Solution
Step 1: Slide window and find max for each window
Windows and max values: [4,2,12,3] -> 12, [2,12,3,8] -> 12, [12,3,8,7] -> 12, [3,8,7,9] -> 9.Step 2: Collect max values in order
The output array is [12, 12, 12, 9].Final Answer:
[12, 12, 12, 9] -> Option AQuick Check:
Max per window = [12,12,12,9] [OK]
- Misreading first window max
- Confusing window elements
- Selecting output with wrong max values
Ready to prove it?
Pass the test and this pattern moves unknown
