workshop private

← all creations

Canonical

viz · created 2026-10-05

The LC 71 Simplify Path walk drawn as the stack it is — a Unix path consumed left to right, a column of folder plates rising beside it, every component doing exactly one of three things (push, nothing, pop), and ".." at the root popping nothing without a special branch. The answer is never edited; it is whatever the stack holds when the input runs out. Two switchable bugs — an unguarded pop that throws, and a rule that reads "..." as "up".

algorithmsinterview-prepcanvas

Simplify Path (LC 71): given an absolute Unix-style path, return its canonical form — one leading slash, no trailing slash, no . or .. components, runs of slashes collapsed. The piece splits the path on / and consumes the components left to right, and the thing to watch is that every component does exactly one of three things to a stack of directory names: a name is pushed, "" and . do nothing, and .. pops the top. That is the whole algorithm. The rule everybody writes a special branch for — .. when there is nothing to pop — is the same rule with nothing to do: the root’s parent is the root, so the stack stays empty and the walk moves on.

The path runs across the top as character tiles, the component being consumed lit white, and under each component the glyph for what it did: ▲ pushed, ▼ popped, ○ popped nothing, · nothing. Empty components get a glyph too, sitting in the gap between two slashes, so // is visibly a component that did nothing rather than a case that was handled. On the right the stack rises from the root line as a column of plates; a pop draws the plate that just left as a dashed ghost above the top, and a pop with nothing to pop draws a dashed outline where a plate would have been.

The answer is never edited. Under the legend is the path the stack implies right now — / plus the plates joined by / — and it changes only because the stack did. When the input runs out that line becomes the answer, and there was no second pass to fix up slashes or strip a trailing one: the canonical form is a property of the stack, not of the string.

Three rules, switchable, because the second and third are what gets written:

Type any absolute path into the box (letters, digits, ., _, -, /), or click any component to jump the walk to it.

Reuse

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

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

Gotchas