What is the output of the following code that finds the maximum sum of any subarray of size 3 using sliding window?
DSA Javascript
const arr = [2, 1, 5, 1, 3, 2];
const k = 3;
let maxSum = 0;
let windowSum = 0;
for (let i = 0; i < k; i++) {
windowSum += arr[i];
}
maxSum = windowSum;
for (let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
if (windowSum > maxSum) maxSum = windowSum;
}
console.log(maxSum);
A9
B7
C8
D6
Attempts:
2 left
š” Hint
Add first k elements, then slide window by removing left and adding right element.
ā Incorrect
The subarrays of size 3 are [2,1,5], [1,5,1], [5,1,3], [1,3,2]. Their sums are 8, 7, 9, 6 respectively. The maximum sum is 9.
ā Predict Output
intermediate
2:00remaining
Output of sliding window average calculation
What is the output of this code that calculates the average of every subarray of size 4?
DSA Javascript
const arr = [1, 3, 2, 6, -1, 4, 1, 8, 2];
const k = 4;
let windowSum = 0;
const result = [];
for (let i = 0; i < k; i++) {
windowSum += arr[i];
}
result.push(windowSum / k);
for (let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
result.push(windowSum / k);
}
console.log(result);
A[3, 2.5, 2.75, 2.5, 3, 3.5]
B[3, 2.5, 3, 2, 3, 4]
C[3, 3, 3, 3, 3, 3]
D[2, 3, 4, 5, 6, 7]
Attempts:
2 left
š” Hint
Calculate sum of first 4 elements, then slide window by subtracting left and adding right element, dividing by k each time.
ā Incorrect
The averages of subarrays of size 4 are: (1+3+2+6)/4=3, (3+2+6-1)/4=2.5, (2+6-1+4)/4=2.75, (6-1+4+1)/4=2.5, (-1+4+1+8)/4=3, (4+1+8+2)/4=3.5.
š§ Conceptual
advanced
2:00remaining
Sliding window technique for variable window size
Which of the following statements about sliding window technique with variable window size is TRUE?
AIt only works if the window size is fixed and known in advance.
BIt cannot be used for problems involving sums or counts of elements.
CIt always requires nested loops to adjust window size dynamically.
DIt uses two pointers that move independently to expand and shrink the window based on conditions.
Attempts:
2 left
š” Hint
Think about how two pointers can move to adjust the window size without nested loops.
ā Incorrect
Variable size sliding window uses two pointers (start and end) that move independently to expand or shrink the window based on problem conditions, avoiding nested loops.
š§ Debug
advanced
2:00remaining
Identify the error in sliding window code
What error will this code produce when trying to find max sum of subarray of size 3?
DSA Javascript
const arr = [1, 2, 3, 4, 5];
const k = 3;
let maxSum = 0;
let windowSum = 0;
for (let i = 0; i < k; i++) {
windowSum += arr[i];
}
maxSum = windowSum;
for (let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
if (windowSum > maxSum) maxSum = windowSum;
}
console.log(maxSum);
ATypeError: Cannot read property 'undefined' of undefined
BOutput: 12
CReferenceError: i is not defined
DOutput: 9
Attempts:
2 left
š” Hint
Check the loop condition for summing first k elements carefully.
ā Incorrect
The first loop runs from i=0 to i<=k (i=3), accessing arr[3] which is valid, but the window size is 3, so it should run i
š Application
expert
3:00remaining
Find smallest subarray with sum >= target
Given an array and a target sum, which code snippet correctly finds the length of the smallest contiguous subarray whose sum is at least the target using sliding window?
A
let minLen = Infinity;
let sum = 0;
let start = 0;
for (let end = 0; end < arr.length; end++) {
sum += arr[end];
while (sum > target) {
minLen = Math.min(minLen, end - start + 1);
sum -= arr[start];
start++;
}
}
return minLen === Infinity ? 0 : minLen;
B
let minLen = 0;
let sum = 0;
for (let start = 0; start < arr.length; start++) {
sum = 0;
for (let end = start; end < arr.length; end++) {
sum += arr[end];
if (sum >= target) {
minLen = Math.min(minLen, end - start + 1);
break;
}
}
}
return minLen;
C
let minLen = Infinity;
let sum = 0;
let start = 0;
for (let end = 0; end < arr.length; end++) {
sum += arr[end];
while (sum >= target) {
minLen = Math.min(minLen, end - start + 1);
sum -= arr[start];
start++;
}
}
return minLen === Infinity ? 0 : minLen;
D
let minLen = Infinity;
let sum = 0;
let start = 0;
for (let end = 0; end < arr.length; end++) {
sum += arr[end];
if (sum >= target) {
minLen = Math.min(minLen, end - start + 1);
sum -= arr[start];
start++;
}
}
return minLen === Infinity ? 0 : minLen;
Attempts:
2 left
š” Hint
Use two pointers and shrink window while sum is at least target.
ā Incorrect
Option C correctly uses a while loop to shrink the window as long as sum >= target, updating minLen. Option C uses nested loops (not sliding window). Option C shrinks window only once per iteration, missing cases. Option C uses sum > target, missing sum == target cases.
Practice
(1/5)
1. What is the main purpose of the sliding window technique when working with arrays?
easy
A. To find the maximum element in the entire array
B. To sort the array elements in ascending order
C. To reverse the array elements in place
D. To efficiently process subarrays of fixed size without recomputing everything
Solution
Step 1: Understand the sliding window concept
The sliding window technique moves a fixed-size window over the array to process subarrays efficiently.
Step 2: Compare with other options
Sorting, reversing, or finding max in the entire array are different operations not related to sliding window.
Final Answer:
To efficiently process subarrays of fixed size without recomputing everything -> Option D
Hint: Update sum by subtracting left, adding right element [OK]
Common Mistakes:
Not updating maxSum correctly
Adding instead of subtracting old element
Wrong initial sum calculation
4. Identify the bug in the following sliding window code that finds the maximum sum of subarrays of size k:
function maxSlidingWindowSum(arr, k) {
let maxSum = 0;
let windowSum = 0;
for(let i = 0; i <= k; i++) {
windowSum += arr[i];
}
maxSum = windowSum;
for(let i = k; i < arr.length; i++) {
windowSum += arr[i] - arr[i - k];
if(windowSum > maxSum) maxSum = windowSum;
}
return maxSum;
}
medium
A. The subtraction in the second loop is incorrect
B. The first loop should run while i < k, not i <= k
C. maxSum should be initialized to Number.MIN_VALUE
D. The function does not handle empty arrays
Solution
Step 1: Check the first loop range
The first loop runs from i = 0 to i <= k, which means it accesses arr[k], an out-of-bound index for window size k.
Step 2: Correct the loop condition
The loop should run while i < k to sum exactly k elements starting from index 0.
Final Answer:
The first loop should run while i < k, not i <= k -> Option B
Quick Check:
Loop range for first window = i < k [OK]
Hint: Loop for first window sums exactly k elements: i < k [OK]
Common Mistakes:
Using <= k causes out-of-bounds error
Ignoring array length checks
Incorrect initialization of maxSum
5. Given an array of integers and a number k, how would you find the length of the longest subarray with sum less than or equal to k using the sliding window technique?
hard
A. Calculate prefix sums and find max subarray sum using Kadane's algorithm
B. Sort the array and use binary search to find subarrays with sum ⤠k
C. Expand the window by moving right pointer and shrink from left when sum exceeds k, track max length
D. Use nested loops to check all subarrays and pick the longest with sum ⤠k
Solution
Step 1: Use two pointers to create a sliding window
Start with both pointers at the beginning, expand right pointer to add elements to sum.
Step 2: Shrink window from left when sum exceeds k
If sum > k, move left pointer forward to reduce sum until sum ⤠k again, tracking max window length.
Step 3: Track the maximum length of valid windows
Keep updating max length whenever sum ⤠k during expansion and shrinking.
Final Answer:
Expand the window by moving right pointer and shrink from left when sum exceeds k, track max length -> Option C
Quick Check:
Sliding window with two pointers and sum check = Expand the window by moving right pointer and shrink from left when sum exceeds k, track max length [OK]
Hint: Move right to expand, left to shrink when sum > k [OK]
Common Mistakes:
Using sorting or binary search which doesn't apply here
Using nested loops causing O(n²) time
Confusing Kadane's algorithm with sum limit problem
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.