Sliding window
Python Sliding Window Interview Questions
A sliding window is a range that moves along a sequence while you keep a total or a count up to date at its edges. It is the standard way to answer anything phrased as "over any seven days".
What sliding window questions ask you to do
Across the questions here, these are the shapes that come up:
- Find the best window of a fixed width
- Find the longest window that still meets a condition
- Work out a moving average
- Find the shortest window containing everything required
Common mistakes with sliding window
Recomputing the whole window at every step makes it exactly as slow as checking every range by hand. Add what comes in and subtract what goes out instead, and settle up front whether the window is a fixed width or one that grows until a condition breaks.
Questions to practice
Questions last reviewed 19 October 2026.