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:
- Is the answer about a contiguous subarray or substring? If no → not sliding window.
- Is the window fixed length k or variable until a condition breaks?
- 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
| Variant | Cue words | Structure |
|---|---|---|
| Fixed length | “every subarray of size k”, max sum of k | grow 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 K | often “at most K” minus “at most K-1” | two passes or careful counts |
| Cover / need | minimum window substring | need 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
- Running sum for numeric windows
- Frequency map or fixed array for characters
- Count of distinct keys currently positive
need/have/formedfor coverage problems- Optional deque for sliding-window maximum (monotonic queue)—mention only when the prompt needs O(n) range maxima
Keep state updates symmetric: every add must have a matching remove path when left advances.
When sliding window is the wrong chooser
- Need best non-contiguous subset → DP / greedy
- Indices jump by graph edges → BFS
- Order can be rearranged freely → sort + two pointers sometimes
- Prompt says “subsequence” not “substring” → different algorithms
- Negative numbers with “smallest sum ≥ S” → classic positive-sum shrink logic can break; call that out and pivot
Saying “contiguous” early prevents a fifteen-minute wrong path.
First five minutes
- Confirm contiguous
- Fixed vs variable
- Define invalid/valid in one sentence
- Choose maximize vs minimize length (or count of windows)
- Name the O(n) plan with left/right pointers
- Sketch one line for the frequency map if needed
Worked micro-example (longest without repeat)
Input: abba
- right on first
a,b→ windowab, best 2 - right on second
b→ shrink until no duplicateb→ windowb, then add… continue carefully - Dry-run forces you to decide whether you shrink past the previous
bcorrectly
If you cannot simulate abba, do not start coding yet.
Common boundary bugs
- Off-by-one length:
right-left+1 - Shrinking too late or too early on “exactly K”
- Forgetting to delete map keys when count hits 0 (distinct drifts)
- Updating
bestinside the wrong branch - Assuming sorted input when the array is not
- Mutating
leftwithout updating sum/map
Freeze recovery
- Write
left = 0and empty state on the pad - Process one example by hand with a table of left/right/state
- Only then code the while-shrink
- 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
| Session | Focus |
|---|---|
| 1 | Fixed-k max sum |
| 2 | Longest with ≤ K distinct |
| 3 | Minimum window covering t |
| 4 | Mixed 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
- [ ] Contiguous confirmed
- [ ] Fixed vs variable named
- [ ] Valid/invalid invariant spoken
- [ ] Maximize vs minimize clear
- [ ] Map cleanup when counts hit zero
- [ ] Dry-ran length formula on a tricky string
Choose the window shape first—the code is mostly disciplined bookkeeping after that.
Rehearse with an AI interview copilot, then browse commonly reported interview questions.