Challenge - 5 Problems
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of sliding window sum calculation
What is the output of this REST API response when calculating the sum of every 3 consecutive numbers in the list?
Rest API
numbers = [1, 3, 5, 7, 9] window_size = 3 result = [] for i in range(len(numbers) - window_size + 1): window_sum = sum(numbers[i:i+window_size]) result.append(window_sum) print(result)
Attempts:
2 left
💡 Hint
Think about how many sums you get when sliding a window of size 3 over 5 numbers.
✗ Incorrect
The sliding window sums are: 1+3+5=9, 3+5+7=15, 5+7+9=21. So the output list is [9, 15, 21].
🧠 Conceptual
intermediate1:30remaining
Sliding window algorithm main advantage
What is the main advantage of using a sliding window algorithm over a naive approach when processing sequences?
Attempts:
2 left
💡 Hint
Think about how sliding windows reuse previous computations.
✗ Incorrect
Sliding window algorithms reuse previous computations to avoid recalculating sums or other metrics, reducing time complexity.
🔧 Debug
advanced2:30remaining
Identify the error in sliding window max calculation
This code tries to find the maximum value in every sliding window of size 2 but produces wrong output. What is the error?
Rest API
nums = [4, 2, 12, 3] window = 2 max_values = [] for i in range(len(nums) - window): max_values.append(max(nums[i:i+window])) print(max_values)
Attempts:
2 left
💡 Hint
Check how many windows the loop covers compared to the input length.
✗ Incorrect
The loop stops one index too early because it uses len(nums) - window instead of len(nums) - window + 1, missing the last window.
📝 Syntax
advanced2:00remaining
Syntax error in sliding window average calculation
Which option contains the correct syntax to calculate the average of every sliding window of size 4 in a list?
Rest API
data = [10, 20, 30, 40, 50, 60] window_size = 4 averages = [] for i in range(len(data) - window_size + 1): avg = sum(data[i:i+window_size]) / window_size averages.append(avg) print(averages)
Attempts:
2 left
💡 Hint
Check for proper line breaks or statement separators in Python loops.
✗ Incorrect
In Python, each statement inside a loop must be on its own line or separated by a semicolon. Option A correctly uses a newline.
🚀 Application
expert3:00remaining
Sliding window for longest substring without repeating characters
Given the string 'abcabcbb', what is the length of the longest substring without repeating characters using a sliding window approach?
Attempts:
2 left
💡 Hint
Try to find the longest substring where no character repeats by moving the window.
✗ Incorrect
The longest substring without repeating characters is 'abc' with length 3.