Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Sliding Window Maximum
What is the output of the following Java code that finds the maximum in each sliding window of size 3?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { int[] nums = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3; List<Integer> result = new ArrayList<>(); Deque<Integer> deque = new LinkedList<>(); for (int i = 0; i < nums.length; i++) { while (!deque.isEmpty() && deque.peekFirst() <= i - k) { deque.pollFirst(); } while (!deque.isEmpty() && nums[deque.peekLast()] < nums[i]) { deque.pollLast(); } deque.offerLast(i); if (i >= k - 1) { result.add(nums[deque.peekFirst()]); } } System.out.println(result); } }
Attempts:
2 left
💡 Hint
Think about how the deque stores indices of elements and how it removes smaller elements.
✗ Incorrect
The code uses a deque to keep track of indices of elements in the current window. It removes indices that are out of the window and those whose values are smaller than the current element. The maximum for each window is at the front of the deque.
🧠 Conceptual
intermediateSliding Window Sum Calculation
Given an array and a window size k, which formula correctly calculates the sum of the next window using the previous window's sum?
Attempts:
2 left
💡 Hint
Think about removing the element that slides out and adding the new element that slides in.
✗ Incorrect
To move the window forward by one, subtract the element leaving the window (arr[i]) and add the new element entering the window (arr[i + k]).
🔧 Debug
advancedIdentify the Bug in Sliding Window Minimum Code
What error will the following Java code produce when finding the minimum in each sliding window of size 2?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { int[] nums = {4, 2, 12, 3, 5}; int k = 2; List<Integer> result = new ArrayList<>(); Deque<Integer> deque = new LinkedList<>(); for (int i = 0; i < nums.length; i++) { while (!deque.isEmpty() && deque.peekFirst() <= i - k) { deque.pollFirst(); } while (!deque.isEmpty() && nums[deque.peekLast()] > nums[i]) { deque.pollLast(); } deque.offerLast(i); if (i >= k - 1) { result.add(nums[deque.peekFirst()]); } } System.out.println(result); } }
Attempts:
2 left
💡 Hint
Check the condition that removes indices out of the current window.
✗ Incorrect
The condition `deque.peekFirst() < i - k` should be `deque.peekFirst() <= i - k` to correctly remove indices outside the window. The current condition fails to remove the index exactly equal to `i - k`, causing the deque to retain outdated indices and produce incorrect minimum values.
🚀 Application
advancedFind Maximum Average Subarray
Which code snippet correctly finds the maximum average of any subarray of size k in an integer array?
Attempts:
2 left
💡 Hint
Sliding window helps calculate sums efficiently without repeated summation.
✗ Incorrect
Sliding window sums the first k elements, then slides forward by subtracting the element leaving and adding the new element. The max average is updated each time.
❓ Predict Output
expertOutput of Variable Size Sliding Window Code
What is the output of this Java code that finds the length of the longest substring with at most 2 distinct characters?
DSA Java
import java.util.*; public class Main { public static void main(String[] args) { String s = "eceba"; int k = 2; Map<Character, Integer> map = new HashMap<>(); int left = 0, right = 0, maxLen = 0; while (right < s.length()) { char c = s.charAt(right); map.put(c, map.getOrDefault(c, 0) + 1); while (map.size() > k) { char leftChar = s.charAt(left); map.put(leftChar, map.get(leftChar) - 1); if (map.get(leftChar) == 0) { map.remove(leftChar); } left++; } maxLen = Math.max(maxLen, right - left + 1); right++; } System.out.println(maxLen); } }
Attempts:
2 left
💡 Hint
Track the window size while keeping at most 2 distinct characters.
✗ Incorrect
The longest substring with at most 2 distinct characters is "ece" with length 3.
