📖 Scenario: You are analyzing daily temperatures recorded over a week. You want to find the highest temperature in every 3-day window to understand short-term heat peaks.
🎯 Goal: Build a program that uses the sliding window technique to find the maximum temperature in every 3-day window from a list of daily temperatures.
📋 What You'll Learn
Create an array called temperatures with exactly these values: [73, 74, 75, 71, 69, 72, 76]
Create a variable called windowSize and set it to 3
Use a for loop with variable i to slide over temperatures and find the maximum temperature in each window of size windowSize
Store each maximum temperature in an array called maxTemperatures
Print the maxTemperatures array at the end
💡 Why This Matters
🌍 Real World
Sliding window technique is used in weather data analysis, stock price monitoring, and real-time sensor data processing to find trends or peaks in a moving time frame.
💼 Career
Understanding sliding window algorithms helps in roles like data analysis, software development, and system monitoring where efficient data processing is required.
Progress0 / 4 steps
1
Create the temperatures array
Create an array called temperatures with these exact values: [73, 74, 75, 71, 69, 72, 76]
DSA Typescript
Hint
Use const temperatures = [73, 74, 75, 71, 69, 72, 76]; to create the array.
2
Set the window size
Create a variable called windowSize and set it to 3
DSA Typescript
Hint
Use const windowSize = 3; to set the window size.
3
Find maximum temperatures in each sliding window
Create an empty array called maxTemperatures. Use a for loop with variable i to slide over temperatures from index 0 to temperatures.length - windowSize. In each iteration, find the maximum temperature in the current window of size windowSize and add it to maxTemperatures.
DSA Typescript
Hint
Use temperatures.slice(i, i + windowSize) to get the current window and Math.max(...window) to find the maximum.
4
Print the maximum temperatures array
Print the maxTemperatures array using console.log(maxTemperatures)
DSA Typescript
Hint
Use console.log(maxTemperatures) to print the result.
Practice
(1/5)
1. What is the main purpose of the sliding window technique on arrays?
easy
A. To find the maximum element in the entire array
B. To sort the array in ascending order
C. To reverse the elements of the array
D. To efficiently process subarrays of fixed size without repeated work
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: Identify the purpose
It avoids recalculating values for overlapping parts of subarrays, improving performance.
Final Answer:
To efficiently process subarrays of fixed size without repeated work -> Option D
Hint: Add new element, subtract old element for each slide [OK]
Common Mistakes:
Not subtracting the element leaving the window
Incorrect initial sum calculation
Off-by-one errors in loop indices
4. Identify the error in this sliding window code snippet and choose the fix:
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 = windowSum + arr[i] - arr[i + k];
if (windowSum > maxSum) {
maxSum = windowSum;
}
}
console.log(maxSum);
medium
A. Initialize maxSum to Number.MIN_VALUE
B. Change arr[i + k] to arr[i - k] in the second loop
C. Change first loop to run from i = 1 to k
D. Add a check if i + k < arr.length before subtraction
Solution
Step 1: Analyze the sliding window update
The window slides by removing the element leaving the window and adding the new element entering it. The element leaving is at index i - k, not i + k.
Step 2: Correct the subtraction index
Change arr[i + k] to arr[i - k] to correctly remove the leftmost element of the previous window.
Final Answer:
Change arr[i + k] to arr[i - k] in the second loop -> Option B
Quick Check:
Subtract element leaving window at i-k [OK]
Hint: Subtract element at i-k, not i+k, when sliding window [OK]
Common Mistakes:
Using wrong index for element leaving window
Not initializing maxSum correctly
Loop range errors
5. Given an array of integers, write a sliding window algorithm to find the maximum sum of any subarray of size k. Which approach below correctly implements this in TypeScript?
hard
A. Initialize sum of first k elements, then slide window adding new element and subtracting old, updating max sum each time
B. Sort the array and sum the first k elements
C. Use nested loops to sum every subarray of size k and track max sum
D. Add all elements and divide by k to get average, then multiply by k for max sum
Solution
Step 1: Understand the sliding window max sum approach
Calculate sum of first k elements as initial window sum.
Step 2: Slide window across array
For each new element, add it and subtract the element leaving the window, update max sum if current sum is higher.
Step 3: Avoid inefficient methods
Sorting or nested loops are less efficient; averaging does not find max sum.
Final Answer:
Initialize sum of first k elements, then slide window adding new element and subtracting old, updating max sum each time -> Option A
Quick Check:
Sliding window updates sum and tracks max [OK]
Hint: Slide window, update sum and max in one pass [OK]
Common Mistakes:
Using nested loops causing slow performance
Sorting array which changes element order
Calculating average instead of max sum
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.