Explain sliding window.
Skip to solutionKEEP THE
mediumDSA
What is the sliding window technique?
213 views
01
Understand the problem
sliding-window
02
Attempt it yourself
Sketch your approach before reading the solution — that's what interviews test.
Nudge consolestandby
Stuck? Beam a request up — the console returns a conceptual nudge that guides your logic without spoiling the implementation.
03
Study the solution
Sliding window maintains a contiguous range over an array/string, expanding and shrinking it to satisfy a constraint — turning many O(n²) substring/subarray problems into O(n). Examples: longest substring without repeating characters, max sum of size-k window.
Solution ready — 2 min read
Classified // press E to declassify
04
Read the code
Max sum of a fixed window
Run Playgrounddef max_window_sum(arr, k):
window = sum(arr[:k])
best = window
for i in range(k, len(arr)):
window += arr[i] - arr[i - k] # add new, drop old
best = max(best, window)
return best
# --- demo ---
print(max_window_sum([1, 4, 2, 9, 3, 5, 1], 3)) # 17 -> [9,3,5]05
Join the discussion
Discussion (0)
Sign in to join the discussion.
No responses yet. Be the first to share what you think.
Transmission complete // awaiting log
KEEP THE
STREAK ALIVE.
Dossier 90 of 127 decoded in the Data Structures & Algorithms track. One more won't hurt.