Over an ordered sequence, how does a moving window of fixed length differ from a window anchored at the start?
answer
- one answer per position
- which edge is allowed to move
- fixed span against never forgetting
- anchored keeps the first record forever
basics
~20 sA moving window of fixed length keeps its edges a fixed distance apart, so old records drop out as new ones enter. A window anchored at the start never moves its left edge, so each answer covers everything so far.
solid answer
~50 sBoth shapes walk along an ordering and emit one answer per position; they differ only in where the left edge sits. A **moving window of fixed length** holds its two edges a fixed distance apart — a distance declared either as a count of records or as a span of time — so a record that was inside it a hundred positions ago has since fallen out and affects nothing. A **window anchored at the start** pins its left edge to the first record and lets only the right edge advance, so each answer covers everything from the beginning up to that position and nothing is ever discarded. The practical difference is memory. The first gives a local statistic that tracks recent behaviour and forgets an old spike; the second gives a running statistic that an early spike still perturbs, and that grows steadily less sensitive to any one new record as the sequence lengthens.
go deeper
Be able to say which edge moves. A fixed-length window forgets old records; a window anchored at the start does not, and each of its answers covers one more record than the last.
Explain what that does to sensitivity. The anchored statistic becomes progressively harder to move as the sequence grows, while the fixed-length one responds to the recent stretch only and discards everything behind it.
Say which you would pick for a live signal and why, and be ready to explain what a long anchored statistic hides: a level shift that happened recently is barely visible in it, and the output looks perfectly healthy.
The angle is commitment. A window length is a parameter every downstream consumer inherits, so argue for deriving it once from how fast the process actually changes, rather than letting each dashboard pick its own round number.
## One answer per position A window computation walks along an ordering — most often a time ordering, but any stable ordering will serve — and emits **one answer per position**, computed from whatever records the window covers while it is sitting at that position. That is the whole shape of the operation. Everything that distinguishes one window computation from another is a rule about where the window's two edges sit and how they move as the position advances. Two rules dominate, and a third sits between them. The market's words for the first two are a *rolling* window — one that walks along with its length held fixed — and an *expanding* window — one whose left edge is pinned to the first record and never moves. ## A moving window of fixed length Both edges advance together, a fixed distance apart. That distance, the window's length, is declared either as a count of records or as a span of time, and which one you declared changes which records the window covers whenever the observations are not evenly spaced. Its defining property is that this window **forgets**. A record enters at the right edge, contributes to a bounded number of answers, then leaves at the left edge and never affects another one. So: - the answer at each position is a *local* statistic — it describes the recent stretch, not the history; - an extreme reading perturbs a bounded number of answers and then vanishes completely; - the answer follows a genuine change of level within roughly one window length; - early positions have fewer records behind them than the length asks for, so the leading edge of the output needs a rule of its own. ## A window anchored at the start The left edge is pinned to the first record and never moves; only the right edge advances. Each answer therefore covers **everything from the beginning up to and including that position**, and each one covers one more record than the last. Its defining property is that this window **never forgets**: - an early extreme reading is folded into every later answer, its influence shrinking only as the record count grows; - the answer becomes progressively harder to move — by the ten-thousandth position, one new record shifts a running mean by roughly one ten-thousandth of its distance from that mean; - a real change of level late in the sequence is nearly invisible, because the accumulated history outweighs it; - there is no length to choose, which is often why people reach for it, and is also why it answers a different question. ## Side by side | | moving window of fixed length | window anchored at the start | |---|---|---| | left edge | advances with the position | pinned to the first record | | records per answer | fixed by count, or variable by span | grows by one each position | | an old record's influence | drops out entirely | never drops out | | response to a level shift | within about one window length | heavily damped, more so over time | | parameter to choose | the length | none | | question it answers | what is happening lately | what has happened so far | ## A third shape, with no hard left edge Between them sits an **exponentially weighted moving statistic**: a moving statistic in which no record is ever dropped, but each earlier observation contributes with a geometrically smaller weight than the one after it. There is no length, only a decay rate. It behaves like a fixed-length window in that recent records dominate, and like an anchored window in that nothing is discarded outright. It is useful when you want recency without the cliff a hard left edge creates, where one record's departure visibly moves the answer. ## What each one actually costs It is tempting to say a window computation is one pass with a cheap incremental update, so its cost is independent of the length. That holds for **additive statistics** — a sum, a count, a mean — where a running accumulator can add the entering record and subtract the leaving one. It does not hold for **order statistics** such as a median or a quantile, which have no cheap removal step, and it does not hold when the per-position computation is a function you wrote yourself: whether that is invoked once per position over the small set of covered records, or recognised and dispatched to a compiled routine, differs by design and by what you passed. The honest answer is that the cost depends on the statistic and on the path the tool took, not that windows are free. ## Choosing 1. Ask what the number is for. An alert threshold, a control limit or a display of current behaviour wants the fixed-length form; a cumulative total, a to-date average or a best estimate of a level assumed stable wants the anchored form. 2. If you take the fixed length, choose it from how fast the underlying process actually changes rather than from a round number, and write the reason down — every consumer of that column inherits it. 3. If you take the anchored form, understand that you have chosen to give recent records almost no weight once the sequence is long, and confirm that this is what you meant.
- What happens to a window anchored at the start when the very first record is a bad reading?It contaminates every subsequent answer. Its weight is roughly one over the number of records seen so far, so the effect shrinks as the sequence grows but never reaches zero, and it is largest exactly where the output is usually inspected — the first few dozen positions. A fixed-length window has the opposite behaviour: the bad reading distorts a bounded number of answers and then leaves.
- Is there a shape between the two, with no hard left edge?Yes — an exponentially weighted moving statistic. Every earlier observation still contributes, but with a weight that decays geometrically with distance, so recent records dominate without anything being dropped outright. It takes a decay rate rather than a length, and it avoids the visible step a fixed-length window produces when one influential record falls out of the left edge.
- Does the cost per position grow with the length of a moving window?It depends on the statistic and the implementation path. A sum, a count or a mean can be maintained incrementally — add the entering record, subtract the leaving one — so cost is roughly independent of length. A median or a quantile has no cheap removal step. A function you wrote yourself may be invoked once per position, or may be recognised and compiled, and which happens differs by design.
A moving window of fixed length is the rear-view mirror: it shows a fixed stretch of road behind you, and the stretch you passed an hour ago has slid out of it entirely. A window anchored at the start is the odometer: it never forgets a metre you drove, so a slow crawl at the beginning of the trip is still dragging on your average speed hours later.
saying these in an interview costs you the question
- Says a window anchored at the start is just a very long moving window.
- Thinks a moving window's earliest positions are computed over the full length.
- Assumes the two shapes converge to the same answer once enough records exist.
- Believes an old outlier stops affecting a running statistic on its own.
- Treats window length as free because the computation is one pass.