workshop

← all creations

Window

viz · created 2026-09-03

The LC 424 sliding window crawling a character stream with its invariant drawn, not asserted — length, max count and their difference as three bars under the window, and a max count kept as a high-water mark that goes stale while the answer stays right.

algorithmsinterview-prepcanvas

A sliding window walks a string of uppercase letters one character at a time, playing Longest Repeating Character Replacement (LC 424): the longest run of a single letter you can make with at most k replacements. Under the window sit three bars drawn in the same units as the tiles — length, max count (the most frequent letter inside), and length − max against a fence at k — so the invariant every valid window satisfies is a picture you can check by eye instead of a line you take on faith. The right edge takes one character; if the third bar crosses the fence, the left edge steps once, and the window slides instead of shrinking.

The point of the piece is the second bar. In the default mode the max count is a high-water mark: raised when an incoming letter’s count beats it, never lowered when the left edge lets those letters go. So it goes stale — the bar shows the tracked value in hatched purple beyond the live one, the window can be flagged not valid — and the answer still comes out right. The reason is written on screen when it happens: the window’s length was earned by a window that really was valid, and it can only grow again once a live count makes a longer window genuinely valid. Wrong-looking but sound, which is exactly why the O(n) solution is hard to trust on a whiteboard.

Switch the max count to recomputed · shrink until valid to watch the textbook-honest version on the same string: the window is valid after every step, it can shrink, and the readout at the end shows both bookkeepings agreeing on the answer. Type your own string, set k, step through, or let it run.

Reuse

src/window.js is a framework-free ES module:

No rendering or timers in the module; the canvas demo is reference code.

Gotchas