Bird
0
0

Find the bug in this sliding window code:

medium📝 Debug Q7 of 15
Rest API - Rate Limiting and Throttling
Find the bug in this sliding window code:
data = [5, 10, 15]
window_size = 2
result = []
for i in range(len(data) - window_size + 1):
    window = data[i:i+window_size]
    result.append(window)
print(result[2])
ASlicing syntax is incorrect
BThe loop range is off by one
CWindow size is larger than data length
DIndexError because result has only 2 windows, no index 2
Step-by-Step Solution
Solution:
  1. Step 1: Calculate number of windows generated

    With data length 3 and window size 2, number of windows = 2 (indices 0 and 1).
  2. Step 2: Accessing result[2] causes IndexError

    Index 2 is out of range since result has only indices 0 and 1.
  3. Final Answer:

    IndexError because result has only 2 windows, no index 2 -> Option D
  4. Quick Check:

    Accessing out-of-range index causes error [OK]
Quick Trick: Check list length before accessing indices [OK]
Common Mistakes:
  • Assuming result has more windows than it does
  • Ignoring zero-based indexing

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More Rest API Quizzes