</> MAANG.io
coding interview Β· 101

Foundations

Master coding interviews with comprehensive coverage of data structures, algorithms, and problem-solving techniques. Progress from fundamentals to advanced topics with expertly curated content.

0/255 solved 0% complete

Fixed & Variable Windows

What is this?

Picture a frame that slides across a row of numbers, lighting up a stretch of them like a moving spotlight. A window comes in two shapes: a fixed one that stays the same width and just rolls forward, and a variable one that stretches wider or pulls in tighter as the data demands. Either way, you only update what changed at the edges instead of re-scanning everything.

flowchart TD A["A window over the data"] --> B["Fixed width that rolls forward"] A --> C["Variable width that grows and shrinks"] B --> D["Update by adding one and dropping one"] C --> D

πŸ’‘ Fun fact: Fixed-size sliding windows are exactly how moving-average filters work in audio and signal processing β€” smoothing out a noisy sound or stock chart by averaging the last few samples as the window glides along.

πŸ”“ The 4 problems in this chapter are free. Sign in with Google or Microsoft to start solving.


Core idea: A sliding window turns an O(nΒ²) "check every subarray" into one O(n) pass by reusing the previous window's work. Two shapes cover most problems: a fixed-width window that rolls one step at a time (update +entering / βˆ’leaving), and a variable window that expands to grow a quantity, then shrinks to restore a constraint.


The two shapes

Fixed window rolling at constant width k versus variable window expanding right then shrinking left

Fixed windows answer "best/quantity over every length-k span." Variable windows answer "longest/shortest span satisfying a constraint" β€” the right edge always advances; the left edge only moves to keep (or restore) validity, so each index enters and leaves at most once β†’ O(n).


The problems

Problem map: Windows split into fixed width (Maximum Average Subarray I) and variable width (Fruits into Baskets, Minimum Size Subarray Sum, Longest 1's After Deleting One)

  • Maximum Average Subarray I β€” the fixed-window intro: roll the sum in O(1) per step.
  • Fruits into Baskets β€” longest window with at most 2 distinct (the "at most k distinct" template).
  • Minimum Size Subarray Sum β€” shortest window reaching a target; shrink while still valid.
  • Longest Subarray of 1's After Deleting One β€” a window carrying a violation budget (at most one zero).

Key takeaways

  • Reuse, don't recompute β€” the window's answer is one cheap edit from the next.
  • Fixed = roll (+in/βˆ’out); variable = expand then shrink to stay valid.
  • Each index enters/leaves once β‡’ O(n) even with the inner shrink loop.
  • "Longest" shrinks on invalidity; "shortest" shrinks on validity β€” mirror images.
  • Why interviewers love it: it's the cleanest "turn O(nΒ²) into O(n)" move on sequences.

Start here: Maximum Average Subarray I (fixed), then Fruits into Baskets (variable).

Fruits into Baskets

Core idea: Grow the window greedily to the right, one element at a time. Only when the window breaks the constraint (more than 2 distinct types) do you shrink from the left β€” just enough to make it valid again. The longest the window ever gets is your answer. Each element enters once and leaves once β†’ O(n).


Problem, rephrased

You're walking down a single row of fruit trees. Tree i grows fruit of type fruits[i] (think apple, banana, cherry…). You carry two baskets, and each basket can hold only one type of fruit β€” but unlimited quantity of it.

You pick one starting tree, then walk strictly rightward, grabbing one fruit from every tree you pass. You must stop the moment you'd need a third type (you only have two baskets). You want to start at the spot that lets you collect the most fruit total.

Strip away the story and it's this: find the length of the longest contiguous subarray that contains at most 2 distinct values. ("Contiguous" because you can't skip trees β€” you walk a straight row.)

Inputs / outputs

fruits longest valid run answer
[1, 2, 1] [1, 2, 1] 3
[0, 1, 2, 2] [1, 2, 2] 3
[1, 2, 3, 2, 2] [2, 3, 2, 2] 4
[3,3,3,1,2,1,1,2,3,3,4] [1, 2, 1, 1, 2] 5

You return the length of that best run, not the fruits themselves.


Sign in to continue reading

The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.

Sign in to read the full lesson

Sign in to MAANG.io

Use your Google or Microsoft account β€” no password to remember.

Continue with Google Continue with Microsoft

Please accept the terms above to continue.