📖 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 trends.
🎯 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 a slice called temperatures with exactly these values: 73, 74, 75, 71, 69, 72, 76, 73
Create an integer variable called windowSize and set it to 3
Use a for loop with variable i to slide over temperatures and find the maximum in each window
Store each maximum in a slice called maxTemps
Print the maxTemps slice at the end
💡 Why This Matters
🌍 Real World
Sliding window technique is used in weather data analysis, stock price monitoring, and network traffic analysis to find trends over fixed intervals.
💼 Career
Understanding sliding window helps in roles like data analysis, software development, and system monitoring where efficient data processing is required.
Progress0 / 4 steps
1
Create the temperatures data
Create a slice called temperatures with these exact values: 73, 74, 75, 71, 69, 72, 76, 73
DSA Go
Hint
Use temperatures := []int{73, 74, 75, 71, 69, 72, 76, 73} to create the slice.
2
Set the window size
Create an integer variable called windowSize and set it to 3
DSA Go
Hint
Use windowSize := 3 to set the size of the sliding window.
3
Find maximum temperatures in each window
Use a for loop with variable i to slide over temperatures from index 0 to len(temperatures) - windowSize. Inside the loop, find the maximum temperature in the current window and append it to a slice called maxTemps. Initialize maxTemps as an empty slice of integers before the loop.
DSA Go
Hint
Use two loops: outer loop slides the window, inner loop finds max in the window.
4
Print the maximum temperatures slice
Print the maxTemps slice using fmt.Println(maxTemps)
DSA Go
Hint
Use fmt.Println(maxTemps) to print the slice.
Practice
(1/5)
1. What is the main purpose of the sliding window technique on arrays?
easy
A. To efficiently process subarrays of fixed size without re-computing everything
B. To sort the array in ascending order
C. To reverse the array elements
D. To find the maximum element in the entire array
Solution
Step 1: Understand sliding window concept
The sliding window technique moves a fixed-size window over the array to process subarrays efficiently.
Step 2: Identify purpose
It avoids recalculating values for overlapping parts of subarrays, improving performance.
Final Answer:
To efficiently process subarrays of fixed size without re-computing everything -> Option A
Hint: Sliding window avoids repeated work on overlapping parts [OK]
Common Mistakes:
Confusing sliding window with sorting
Thinking it reverses arrays
Assuming it finds max of whole array
2. Which of the following Go code snippets correctly initializes a sliding window sum for the first 3 elements of an integer array arr?
easy
A. sum := 0
for i := 0; i < 3; i++ {
sum += arr[i]
}
B. sum := 0
for i := 1; i <= 3; i++ {
sum += arr[i]
}
C. sum := 0
for i := 0; i <= 3; i++ {
sum += arr[i]
}
D. sum := 0
for i := 1; i < 3; i++ {
sum += arr[i]
}
Solution
Step 1: Check loop bounds for first 3 elements
Array indices start at 0, so first 3 elements are at indices 0,1,2. Loop must run i from 0 to less than 3.
Step 2: Verify each option
sum := 0
for i := 0; i < 3; i++ {
sum += arr[i]
} loops i=0 to 2 correctly. Others either start at 1 or go out of bounds.
Final Answer:
sum := 0
for i := 0; i < 3; i++ {
sum += arr[i]
} -> Option A
Quick Check:
Loop i=0 to 2 sums first 3 elements [OK]
Hint: Array indices start at 0; loop to i < window size [OK]
Common Mistakes:
Starting loop at 1 instead of 0
Using <= instead of < causing out of bounds
Incorrect loop end index
3. What is the output of the following Go code?
arr := []int{2, 1, 5, 1, 3, 2}
windowSize := 3
sum := 0
for i := 0; i < windowSize; i++ {
sum += arr[i]
}
result := []int{sum}
for i := windowSize; i < len(arr); i++ {
sum += arr[i] - arr[i-windowSize]
result = append(result, sum)
}
fmt.Println(result)
Hint: Add new element, subtract old element to update sum [OK]
Common Mistakes:
Not subtracting the element leaving the window
Appending sum before updating
Incorrect initial sum calculation
4. Identify the bug in this Go code that tries to find the maximum sum of any subarray of size 2:
arr := []int{1, 3, 2, 5, 1}
windowSize := 2
maxSum := 0
sum := 0
for i := 0; i < windowSize; i++ {
sum += arr[i]
}
for i := windowSize; i < len(arr); i++ {
sum += arr[i] - arr[i-windowSize]
if sum > maxSum {
maxSum = sum
}
}
fmt.Println(maxSum)
medium
A. maxSum should be initialized to a very small number, not 0
B. The first window sum is not compared to maxSum
C. The loop should start from i = 0 in the second loop
D. The subtraction in sum update is incorrect
Solution
Step 1: Check initial maxSum and first window sum
maxSum is 0, sum is sum of first 2 elements (1+3=4). But maxSum is not updated with this initial sum.
Step 2: Identify missing comparison
The first window sum should be compared to maxSum before sliding the window, else maxSum remains 0 if all sums are less.
Final Answer:
The first window sum is not compared to maxSum -> Option B
Quick Check:
Compare initial sum to maxSum before loop [OK]
Hint: Always compare initial window sum to max before sliding [OK]
Common Mistakes:
Not updating maxSum with initial sum
Wrong loop start index
Incorrect sum update formula
5. Given an array of integers, write a Go function using sliding window to find the length of the longest subarray with sum less than or equal to a target value k. Which approach correctly solves this problem?
hard
A. Sort the array and use binary search to find subarrays with sum ≤ k
B. Use a stack to store sums and pop when sum exceeds k
C. Calculate sum of all subarrays and pick the longest with sum ≤ k
D. Use two pointers to expand and shrink the window, updating sum and max length accordingly
Solution
Step 1: Understand problem constraints
We want the longest subarray with sum ≤ k, which requires dynamic window size adjustment.
Step 2: Choose sliding window with two pointers
Expand right pointer adding elements to sum; if sum > k, move left pointer to shrink window until sum ≤ k; track max length.
Final Answer:
Use two pointers to expand and shrink the window, updating sum and max length accordingly -> Option D
Quick Check:
Two-pointer sliding window adjusts size for sum ≤ k [OK]
Hint: Expand right, shrink left when sum exceeds target [OK]
Common Mistakes:
Sorting array breaks subarray order
Checking all subarrays is inefficient
Using stack is not suitable here
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.