Sliding Window Interview Pattern Chooser

Sliding Window Interview Pattern Chooser

Sliding window interview problems feel similar until they are not. Fixed-size windows, variable windows with a validity condition, and “at most / at least K distinct” variants need different shrink rules. This chooser helps you pick the right window shape fast and narrate it clearly under time pressure.

The 10-second classification

Ask three questions before you touch the keyboard:

  1. Is the answer about a contiguous subarray or substring? If no → not sliding window.
  2. Is the window fixed length k or variable until a condition breaks?
  3. Does the condition care about sum, counts/distinct characters, or coverage of a need set?

Talk track:

“We need a contiguous segment; I’ll maintain a window [left, right] and move right, shrinking left when the invariant breaks.”

That sentence tells the interviewer you are not guessing—you have an invariant.

Pattern menu

VariantCue wordsStructure
Fixed length“every subarray of size k”, max sum of kgrow to k, slide both ends
Variable + at most“longest with at most K distinct”expand right; shrink while invalid
Variable + at least“smallest with sum ≥ target”expand until valid; shrink to minimize
Exactly Koften “at most K” minus “at most K-1”two passes or careful counts
Cover / needminimum window substringneed map + have/formed counters

If the problem is non-contiguous subsequences, stop—window is the wrong family. Say “subsequence vs substring” out loud if the prompt is ambiguous.

Fixed window template

init sum/state for first k
best ← state
for i from k .. n-1:
  add a[i]; remove a[i-k]
  update best

Narrate the invariant: “window always covers exactly k elements.” Fixed windows are easier to dry-run; start here in practice even if your target problem is variable.

Variable window template (longest valid)

left ← 0
for right in 0..n-1:
  add a[right] into state
  while window invalid:
    remove a[left]; left += 1
  update best with length (right-left+1)

Use this when you maximize length under a constraint (“longest substring without repeating characters,” “at most K distinct”).

Variable window template (shortest valid)

left ← 0
for right in 0..n-1:
  add a[right]
  while window valid AND can shrink:
    update best with length
    remove a[left]; left += 1

Know which you are optimizing: maximize length under constraint vs minimize length that satisfies constraint. Mixing them is a common silent bug that still “looks like” sliding window.

State you usually keep

Keep state updates symmetric: every add must have a matching remove path when left advances.

When sliding window is the wrong chooser

Saying “contiguous” early prevents a fifteen-minute wrong path.

First five minutes

  1. Confirm contiguous
  2. Fixed vs variable
  3. Define invalid/valid in one sentence
  4. Choose maximize vs minimize length (or count of windows)
  5. Name the O(n) plan with left/right pointers
  6. Sketch one line for the frequency map if needed

Worked micro-example (longest without repeat)

Input: abba

If you cannot simulate abba, do not start coding yet.

Common boundary bugs

Freeze recovery

  1. Write left = 0 and empty state on the pad
  2. Process one example by hand with a table of left/right/state
  3. Only then code the while-shrink
  4. If stuck on “exactly K,” implement an at-most helper first

Talk track:

“I’ll get ‘at most K’ correct, then derive ‘exactly K’ as atMost(K) − atMost(K−1).”

Practice plan

SessionFocus
1Fixed-k max sum
2Longest with ≤ K distinct
3Minimum window covering t
4Mixed timed: classify only, then code one

Soft practice note

Window problems punish rushed shrinks more than weak theory. If you want a Chrome side panel to rehearse the classify → invariant → expand/shrink talk track while you practice, Luna Interview is built for that calm loop. Use it for composure under pressure—not as a substitute for pattern reps or for anything that violates interview rules.

Day-of checklist

Choose the window shape first—the code is mostly disciplined bookkeeping after that.