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 of Size K
What is the output of the following C++ code that finds the maximum sum of any subarray of size 3?
DSA C++
int arr[] = {2, 1, 5, 1, 3, 2}; int k = 3; int max_sum = 0, window_sum = 0; for (int i = 0; i < k; i++) { window_sum += arr[i]; } max_sum = window_sum; for (int i = k; i < 6; i++) { window_sum += arr[i] - arr[i - k]; if (window_sum > max_sum) { max_sum = window_sum; } } std::cout << max_sum << std::endl;
Attempts:
2 left
💡 Hint
Think about the sums of all subarrays of size 3 and pick the largest.
✗ 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 Size Subarray Sum
What is the output of this C++ code that finds the minimum length of a contiguous subarray with sum at least 7?
DSA C++
int arr[] = {2, 3, 1, 2, 4, 3}; int target = 7; int min_len = 7, sum = 0, start = 0; for (int end = 0; end < 6; end++) { sum += arr[end]; while (sum >= target) { min_len = std::min(min_len, end - start + 1); sum -= arr[start]; start++; } } if (min_len == 7) min_len = 0; std::cout << min_len << std::endl;
Attempts:
2 left
💡 Hint
Try to find the smallest window where the sum is at least 7.
✗ Incorrect
The subarray [4,3] has sum 7 and length 2, which is the smallest possible.
🔧 Debug
advancedIdentify the Bug in Sliding Window Maximum Code
What error will this C++ code produce when trying to find the maximum in each sliding window of size 3?
DSA C++
int arr[] = {1, 3, -1, -3, 5, 3, 6, 7}; int k = 3; std::deque<int> dq; for (int i = 0; i < 8; i++) { while (!dq.empty() && arr[dq.back()] < arr[i]) { dq.pop_back(); } if (!dq.empty() && dq.front() <= i - k) { dq.pop_front(); } dq.push_back(i); if (i >= k - 1) { std::cout << arr[dq.front()] << " "; } } std::cout << std::endl;
Attempts:
2 left
💡 Hint
Check the order of conditions when popping from the front of the deque.
✗ Incorrect
The condition to pop from front should be checked before pushing new elements. Here, it is checked after, causing invalid access when dq.front() is out of window.
🧠 Conceptual
advancedSliding Window Technique Explanation
Which of the following best describes the sliding window technique on arrays?
Attempts:
2 left
💡 Hint
Think about how a window moves over an array without reprocessing all elements.
✗ Incorrect
Sliding window uses two pointers to maintain a window that slides over the array, allowing efficient calculation of sums, maximums, or other properties of contiguous elements.
🚀 Application
expertFind Number of Subarrays with Sum Exactly K
Given an integer array and an integer k, which approach correctly counts the number of contiguous subarrays that sum exactly to k using a sliding window or prefix sums?
DSA C++
int countSubarraysWithSumK(const std::vector<int>& nums, int k) { int count = 0, sum = 0; std::unordered_map<int, int> prefixSumCount; prefixSumCount[0] = 1; for (int num : nums) { sum += num; if (prefixSumCount.find(sum - k) != prefixSumCount.end()) { count += prefixSumCount[sum - k]; } prefixSumCount[sum]++; } return count; }
Attempts:
2 left
💡 Hint
Consider how prefix sums and hash maps help count subarrays with exact sums.
✗ Incorrect
The code uses prefix sums and a hash map to count how many previous sums equal current sum minus k, thus counting subarrays summing to k efficiently even with negative numbers.
