workshop private

← all creations

Retrace

viz · created 2026-09-27

The LC 79 Word Search grid DFS drawn one event at a time — the live path lit through the letters, every neighbour it turns away flagged with why — with the visited mark's lifetime and the outer scan each switchable to their bug, so a cell that stays marked after its branch is abandoned is a thing you watch turn a true into a false.

algorithmsinterview-prepcanvas

Word Search (LC 79): a grid of letters, a word, and the question of whether the word can be spelled along a path of side-adjacent cells that uses no cell twice. The search is a depth-first walk from every cell whose letter matches the first one, and the piece draws that walk as it happens, one event per step: pick a start, enter a cell that matches, reject a neighbour (off the board, wrong letter, already used), spell the word, pop back out. The path the recursion is standing on is lit blue with its depth in the corner; the cell this step is about wears a white ring; the word on the right fills in as far as the path spells it.

Two controls, one per classic failure.

Visited mark is the whole problem. “May not be used more than once” makes the visited set a property of the current path, not of the grid, so the mark a cell gets on the way in has to come off on the way back out. Cleared on return is that. Never cleared is a global visited set: abandoned branches leave their cells hatched amber, nobody standing on them, and a later path that needs one is turned away as if something were — the AAB preset goes from true to false and the readout says false negative. No marks is the other direction: the brief’s ABCB comes out true along a path that steps on B twice, and the badge in that cell reads 2·4.

Start cells is the scan bug. Try every match keeps going when a search fails. Return from the first match is if board[r][c] == word[0]: return dfs(r, c, 0) — it returns whatever the first matching cell says, and SEE on the brief’s board fails from the first S without ever trying the second.

Type your own board (rows separated by spaces, up to 6 × 6) and word (up to 15 letters, capped so the page stays responsive — a search past 4 000 events is truncated and says so), step through, or let it run.

Reuse

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

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

Gotchas