workshop private

← all creations

Ledger

viz · created 2026-09-08

The LC 560 prefix-sum walk with its hash map drawn — a running balance line over the array, a tally of every prefix value seen, and each step's lookup for P − k lit as the subarrays it counts. Switch to a sliding window and watch one negative number break it.

algorithmsinterview-prepcanvas

An integer array is taken one element at a time, playing Subarray Sum Equals K (LC 560): how many contiguous subarrays sum to exactly k. Under the tiles runs the prefix line — P[j] drawn on the boundary between tiles, so P[i+1] − P[j] being the sum of tiles j..i is a picture rather than an identity. Under that sits the map: one bar per prefix value seen, stacked once per sighting. Each step extends the balance, draws a dashed line at P − k, lights every earlier prefix sitting on that line, and draws a green bracket under each subarray those prefixes name — that is the count going up. Only then is the current prefix recorded, which is why a k = 0 step never matches itself.

The piece has two other bookkeepings on the same array. Unseeded leaves the empty prefix {0: 1} out of the map: every step runs identically until a prefix lands exactly on k, and the subarray starting at index 0 goes by as a dashed red bracket with the reason written beside it. Sliding window runs the reflex answer — take on the right, shrink from the left while the sum is over k, count on equality — and it is correct on an all-positive array and wrong on almost any other: with a negative inside, taking an element can lower the sum and dropping one can raise it, so “sum > k, shrink” walks straight past matches. The missed brackets pile up in red under the running count while the true answer sits beside it. Hit positives only and the same window agrees with the map at every step, which is the precise property the interviewer is asking you to name.

Type your own array and k, step through, or let it run.

Reuse

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

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

Gotchas