0
0
DSA Pythonprogramming~20 mins

Sliding Window on Arrays in DSA Python - Practice Problems & Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Sliding Window Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of sliding window maximum sum
What is the output of the following code that finds the maximum sum of any subarray of size 3 using sliding window?
DSA Python
arr = [2, 1, 5, 1, 3, 2]
window_size = 3
max_sum = 0
window_sum = sum(arr[:window_size])
max_sum = window_sum
for i in range(window_size, len(arr)):
    window_sum += arr[i] - arr[i - window_size]
    if window_sum > max_sum:
        max_sum = window_sum
print(max_sum)
A9
B8
C7
D6
Attempts:
2 left
💡 Hint
Think about how the window slides and updates the sum by adding the new element and removing the old one.
Predict Output
intermediate
2:00remaining
Output of minimum average subarray
What is the output of this code that finds the minimum average of any subarray of size 4?
DSA Python
arr = [4, 2, 2, 5, 1, 5, 8]
window_size = 4
window_sum = sum(arr[:window_size])
min_sum = window_sum
for i in range(window_size, len(arr)):
    window_sum += arr[i] - arr[i - window_size]
    if window_sum < min_sum:
        min_sum = window_sum
min_avg = min_sum / window_size
print(min_avg)
A3.25
B2.5
C3.0
D4.0
Attempts:
2 left
💡 Hint
Calculate sums of each 4-element window and find the smallest sum, then divide by 4.
🔧 Debug
advanced
2:00remaining
Identify the error in sliding window code
What error does this code raise when trying to find the maximum sum of subarrays of size 3?
DSA Python
arr = [1, 2, 3, 4, 5]
window_size = 3
max_sum = 0
window_sum = sum(arr[:window_size])
for i in range(window_size, len(arr)):
    window_sum += arr[i] - arr[i - window_size]
    if window_sum > max_sum:
        max_sum = window_sum
print(max_sum)
ANo error, output is 12
BIndexError: list index out of range
CNameError: name 'max_sum' is not defined
DTypeError: unsupported operand type(s)
Attempts:
2 left
💡 Hint
Check if all variables are defined and indices are valid.
🚀 Application
advanced
2:00remaining
Find the count of subarrays with sum equal to k
Given an array and a number k, which option correctly counts the number of continuous subarrays whose sum equals k using sliding window?
DSA Python
arr = [1, 1, 1, 2, 3]
k = 3
count = 0
start = 0
current_sum = 0
for end in range(len(arr)):
    current_sum += arr[end]
    while current_sum > k and start <= end:
        current_sum -= arr[start]
        start += 1
    if current_sum == k:
        count += 1
print(count)
A2
B1
C4
D3
Attempts:
2 left
💡 Hint
Think about how the window shrinks when sum exceeds k and counts when sum equals k.
🧠 Conceptual
expert
2:00remaining
Why sliding window fails for negative numbers in subarray sum problem?
Why does the sliding window technique fail to find the count of subarrays with sum equal to k if the array contains negative numbers?
ABecause sliding window cannot handle arrays longer than 100 elements
BBecause negative numbers cause index errors in sliding window
CBecause sliding window assumes all numbers are positive to shrink window correctly
DBecause sliding window requires sorted arrays
Attempts:
2 left
💡 Hint
Think about how the window shrinks when sum exceeds k and how negative numbers affect this logic.