
Python practice
Python sliding window practice questions
11 problems, ordered from easiest. They run in your browser, and 1 is free.
Start with Best 3-day sales windowThe idea in a minute
A sliding window looks at a block of neighbouring items and moves it one step at a time. Instead of recalculating the whole block each step, you add the item that enters and remove the item that leaves.
That turns a slow nested loop into a single pass. The problems begin with a fixed window size and then move to windows that grow and shrink.
Example: Largest sum of 3 in a row
nums = [2, 1, 5, 1, 3, 2]
k = 3
window = sum(nums[:k])
best = window
for i in range(k, len(nums)):
window += nums[i] - nums[i - k]
best = max(best, window)
print(best)What it prints
9What to watch for
- Update the window total in one step: add the new item, subtract the old one.
- Be careful with the first window, which needs to be built before the loop starts.
- For a window that changes size, decide exactly when it is allowed to shrink.
Sliding Window problems
- Best 3-day sales windowintermediate · Free
- Longest Substring No Repeatintermediate · Locked
- Min Window Substringintermediate · Locked
- Longest Repeating Char Replaceintermediate · Locked
- Permutation in Stringintermediate · Locked
- Find All Anagramsintermediate · Locked
- Sliding Window Maxintermediate · Locked
- Longest Substring K Distinctintermediate · Locked
- Find Anagrams Windowintermediate · Locked
- Longest streak of active daysintermediate · Locked
- Sliding window unique visitor countadvanced · Locked
Want a guided order across every topic? Open the practice sheets or the Python hub.