Skip to contentSkip to editor
All posts
4 min read

Sliding Window Pattern Interview Guide

Learn how to spot sliding-window questions, maintain the right invariants, and explain your logic clearly in coding interviews.

sliding windowleetcodepatternsinterview prep

Sliding window is the pattern that turns many "scan all subarrays" problems from impossible under interview time into clean O(n) solutions.

Yet candidates still struggle with it because they memorize templates without understanding the control logic: when to expand, when to shrink, and what invariant has to stay true.

This guide is about the control logic.

The core idea in one sentence

Maintain a contiguous window that satisfies a condition, expand to explore, and shrink only when the condition breaks or can be improved.

That sentence sounds simple, but it encodes most of what you need.

Recognition cues: when sliding window is likely

Sliding window is a strong candidate when the problem asks for:

  • Longest/shortest contiguous subarray or substring
  • Count of subarrays with constraints
  • Max/min value over a moving contiguous range
  • "At most K distinct" or "no repeating chars" conditions

If the word contiguous appears, your pattern radar should light up immediately.

Fixed vs variable windows

Fixed-size window

Window size k is given. You move right one step at a time and update state incrementally.

Use for:

  • Maximum sum subarray of size k
  • Moving averages

Variable-size window

Window grows and shrinks based on a validity condition.

Use for:

  • Longest substring without repeating chars
  • Minimum window containing required chars
  • Longest subarray with sum <= target (under suitable constraints)

Mixing these two models is where many bugs begin. Decide mode first.

Sliding window framework table

Problem shapeWindow typeState you maintain
Exact length kFixedRunning sum / counts
Longest valid spanVariableFrequency map + left pointer
Minimum valid spanVariableRequired counts + match metric
At most K constraintVariableDistinct count / budget

If you cannot name your maintained state, you are not ready to code yet.

Invariant-driven implementation

For variable windows, write this in your head before coding:

  1. What makes a window valid?
  2. What event makes it invalid?
  3. What exact action restores validity?
  4. When do I record/update answer?

Example invariant for "longest substring without repeating characters":

"Window [left..right] contains no duplicate chars."

When duplicate appears at right, move left and decrement counts until invariant is restored.

This is not syntax. It is control flow reasoning.

Canonical example: longest substring without repeating characters

Reasoning sequence:

  1. Brute force over all substrings is O(n^2) or worse.
  2. Use variable window with frequency map.
  3. Expand right, add current char.
  4. While char count exceeds 1, shrink from left.
  5. After restoring validity, update max length.

Complexity: O(n) time, O(min(n, alphabet)) space.

The key to correctness is that each index moves at most once left-to-right, so total pointer movement is linear.

Canonical example: minimum window substring

This is a more advanced variable window pattern and common interview differentiator.

Reasoning:

  1. Track required counts from target string.
  2. Expand right and accumulate counts.
  3. When all requirements are met, shrink left to minimize length while preserving validity.
  4. Record best window whenever valid.

Interview tip: state clearly that your shrinking loop runs only while valid, and validity check depends on matched requirement count, not raw window size.

Common sliding-window mistakes

  1. Updating answer at wrong time - For minimum window, update while valid during shrink loop, not only after expansion.
  2. Forgetting to decrement state on left move - Leads to ghost counts and invalid logic.
  3. Confusing at-most-K with exactly-K - Exactly-K often derived via atMost(K) - atMost(K-1).
  4. Overusing nested loops fear - A nested while inside for can still be O(n) if pointers move monotonically.

Mentioning #4 explicitly in interviews often earns credibility.

Communication pattern that works in interviews

Use this concise narration:

"I will maintain a window [left..right] and a frequency map. Invariant: window remains valid under [condition]. I expand right each step, and if invariant breaks, I shrink left until restored. Because each pointer only moves forward, total time is O(n)."

This frames your algorithm before code details overwhelm the conversation.

Practice progression for reliable mastery

Train in this order:

  1. Fixed-size running sum problems
  2. Variable window with uniqueness constraints
  3. At-most-K distinct problems
  4. Minimum valid window problems
  5. Timed mixed set with explanation out loud

Sophocode sessions are effective here because they force explicit invariant writing before implementation. That habit dramatically reduces random pointer bugs.

Practice next

Sliding window feels hard until the invariant clicks. Once it does, a whole class of interview problems becomes structured, predictable, and much faster to solve.