Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediateOutput of Maximum Sum Subarray with Fixed Window Size
What is the output of this C# code that finds the maximum sum of any subarray of size 3 using sliding window?
DSA C#
int[] arr = {2, 1, 5, 1, 3, 2}; int windowSize = 3; int maxSum = 0, windowSum = 0; for (int i = 0; i < windowSize; i++) { windowSum += arr[i]; } maxSum = windowSum; for (int i = windowSize; i < arr.Length; i++) { windowSum += arr[i] - arr[i - windowSize]; if (windowSum > maxSum) maxSum = windowSum; } System.Console.WriteLine(maxSum);
Attempts:
2 left
💡 Hint
Think about the sums of subarrays of size 3: (2+1+5), (1+5+1), (5+1+3), (1+3+2).
✗ Incorrect
The subarrays of size 3 are: [2,1,5] sum=8, [1,5,1] sum=7, [5,1,3] sum=9, [1,3,2] sum=6. The maximum sum is 9.
❓ Predict Output
intermediateOutput of Minimum Length Subarray Sum
What is the output of this C# code that finds the minimum length of a contiguous subarray with sum >= 7?
DSA C#
int[] nums = {2,3,1,2,4,3}; int target = 7; int minLength = int.MaxValue; int sum = 0, left = 0; for (int right = 0; right < nums.Length; right++) { sum += nums[right]; while (sum >= target) { minLength = Math.Min(minLength, right - left + 1); sum -= nums[left]; left++; } } System.Console.WriteLine(minLength == int.MaxValue ? 0 : minLength);
Attempts:
2 left
💡 Hint
Check subarrays with sums >= 7 and find the smallest length.
✗ Incorrect
The subarray [4,3] has sum 7 and length 2, which is the smallest possible length meeting the condition.
🔧 Debug
advancedIdentify the Error in Sliding Window Maximum Code
What error does this C# code produce when trying to find the maximum value in each sliding window of size 3?
DSA C#
int[] nums = {1,3,-1,-3,5,3,6,7}; int k = 3; List<int> result = new List<int>(); for (int i = 0; i <= nums.Length - k; i++) { int max = int.MinValue; for (int j = i; j < i + k; j++) { if (nums[j] > max) max = nums[j]; } result.Add(max); } System.Console.WriteLine(string.Join(",", result));
Attempts:
2 left
💡 Hint
Check the loop boundaries and indexing carefully.
✗ Incorrect
The code correctly iterates over all windows of size 3 and finds the max in each window. It produces output '3,3,5,5,6,7' without errors.
❓ Predict Output
advancedOutput of Count of Subarrays with Sum Equals K
What is the output of this C# code that counts the number of continuous subarrays whose sum equals 3?
DSA C#
int[] nums = {1,1,1}; int k = 3; int count = 0; for (int start = 0; start < nums.Length; start++) { int sum = 0; for (int end = start; end < nums.Length; end++) { sum += nums[end]; if (sum == k) count++; } } System.Console.WriteLine(count);
Attempts:
2 left
💡 Hint
Consider all subarrays and count those with sum exactly 3.
✗ Incorrect
There are three continuous subarrays whose sum equals 3: [1,1,1], [1,1,1] starting at different indices (actually, the subarrays are [1,1,1], [1,1], and [1,1] but sum 3 only for the full array). Actually, the correct count is 1.
🧠 Conceptual
expertMinimum Window Substring Concept
Given a string S and a string T, which sliding window approach correctly finds the minimum window in S which contains all characters of T?
Attempts:
2 left
💡 Hint
Think about expanding and shrinking the window to cover all required characters efficiently.
✗ Incorrect
The correct approach is to expand the right pointer to include all required characters, then shrink from the left to find the smallest window covering T.
