What if you could find the hottest day in every 3-day period without checking each day again and again?
Why Sliding Window Maximum Using Deque in DSA Java?
Imagine you have a long list of daily temperatures, and you want to find the hottest day in every 3-day period. Doing this by checking each 3-day group one by one feels like repeating the same work over and over.
Manually checking each group means looking at many overlapping days multiple times. This is slow and tiring, especially if the list is very long. It's easy to make mistakes or miss the hottest day because you have to remember many values.
The sliding window maximum with a deque helps by keeping track of only the important days. It moves through the list once, quickly updating the hottest day for each group without repeating work. This saves time and avoids confusion.
for (int i = 0; i <= arr.length - k; i++) { int max = arr[i]; for (int j = i; j < i + k; j++) { if (arr[j] > max) max = arr[j]; } System.out.print(max + " "); }
Deque<Integer> deque = new LinkedList<>(); for (int i = 0; i < arr.length; i++) { while (!deque.isEmpty() && arr[deque.peekLast()] <= arr[i]) deque.pollLast(); deque.offerLast(i); if (deque.peekFirst() <= i - k) deque.pollFirst(); if (i >= k - 1) System.out.print(arr[deque.peekFirst()] + " "); }
This method lets you find the maximum in every window quickly, even for huge lists, making real-time data analysis possible.
Streaming services use this to find peak viewer counts in recent time windows to adjust recommendations instantly.
Manual checking repeats work and is slow.
Deque keeps track of useful candidates efficiently.
Sliding window maximum runs fast and uses little memory.
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
