Bird
0
0

Identify the error in the following sliding window code:

medium📝 Debug Q6 of 15
Rest API - Rate Limiting and Throttling
Identify the error in the following sliding window code:
data = [1, 2, 3, 4]
window_size = 3
result = []
for i in range(len(data) - window_size):
    window_sum = sum(data[i:i+window_size])
    result.append(window_sum)
print(result)
AThe window size is too large
BThe range should be len(data) - window_size + 1
Csum() function is used incorrectly
DThe list slicing syntax is wrong
Step-by-Step Solution
Solution:
  1. Step 1: Analyze the loop range for sliding windows

    The loop uses range(len(data) - window_size), which misses the last valid window.
  2. Step 2: Correct the range to include all windows

    It should be range(len(data) - window_size + 1) to cover all windows.
  3. Final Answer:

    The range should be len(data) - window_size + 1 -> Option B
  4. Quick Check:

    Loop range must cover all windows [OK]
Quick Trick: Loop range = len(data) - window_size + 1 [OK]
Common Mistakes:
  • Off-by-one error in loop range
  • Incorrect slicing assumptions

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes