# Sliding-window minimum

> Find the leftmost smallest item in each full window of a sequence. Report the first minimum, then emit a new `(value, position)` pair only when its position changes. This private workload compares rescanning on expiry with a monotonic deque backed by either storage layout from `knotkit/deque`.

Find the leftmost smallest item in each full window of a sequence. Report the first minimum, then emit a new `(value, position)` pair only when its position changes. This private workload compares rescanning on expiry with a monotonic deque backed by either storage layout from `knotkit/deque`.

## Result

> Increasing input at width 128: the array deque takes about 0.48 ms against 8.36 ms for rescanning; on random input rescanning is faster

## Modules

* [`knotkit/deque`](/modules/deque/): Queues elements at both ends in O(1) amortized time, over array and Float64Array ring buffers, with an optional fixed capacity that turns the queue into a sliding window

## Figures

* The minimum expires at every step on this increasing sequence.
* At width 512: 1831935 rescan comparisons and 4095 deque comparisons.
* On seeded random input at width 512: 9705 rescan comparisons and 8164 deque comparisons.
* The deque uses bounded candidate storage to avoid rescanning a whole window.

Hover or focus a chart for exact values. Drawn by [`demo.ts`](https://github.com/jkomyno/knotkit/blob/main/problems/sliding-window-minimum/demo.ts) at build time, from the fixtures the benchmark uses; it reproduces the README figure.

## Reproduce

```bash
git clone https://github.com/jkomyno/knotkit.git && cd knotkit && bun install
bun run demo:problem:sliding-window-minimum
```

[`problems/sliding-window-minimum/README.md`](https://github.com/jkomyno/knotkit/blob/main/problems/sliding-window-minimum/README.md) states the fixture, the seed, and the result table.
