workshop private

← all creations

Pincer

viz · created 2026-09-06

The LC 15 sort-then-two-pointer scan with its cost drawn as a triangle of candidate pairs that every probe carves a whole row or column out of — plus the two ways the duplicate handling goes wrong, run on the same input so you can watch a triplet leak twice or never show up.

algorithmsinterview-prepcanvas

A sorted row of integers, an anchor, and two pointers pinching toward each other, playing 3Sum (LC 15): every distinct triplet that sums to zero. Under the row sits the piece’s argument — the triangle of candidate pairs (lo, hi) for the current anchor, one cell per pair. When the probe’s sum is too small, lo moves right and its entire row goes dark: every pair with this lo and a hi at or left of the current one sums to at most what was just seen. Too big, and hi’s whole column dies for the mirror reason. One comparison, hi − lo pairs gone. The pointers cross after at most n − i − 2 probes, however many pairs the triangle held, which is the O(n) inner scan that turns the whole problem quadratic after an O(n log n) sort.

The rest of the piece is the part of the problem that is actually the interview: the duplicates. A dedup switch runs the same input three ways. Correct skips an anchor that equals the value before it (the previous anchor already found everything starting with that value) and, after a hit, steps each inner pointer past its own repeats — skips show as struck-through tiles and cost no probe. None skips nothing, and the same triplet comes off the belt twice, flagged red in the list. Wrong skips an anchor that equals the value after it, the classic off-by-one direction, and the run ends with a triplet it never found, named in red. All three carve the triangle identically. Only the answers differ.

Two smaller things are drawn rather than asserted: once the anchor turns positive the scan stops, with everything to its right greyed out because nothing there can pull a sum back to zero; and the running probe count sits next to the number of triples brute force would examine, so the quadratic against the cubic is a pair of numbers on screen instead of a claim.

Reuse

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

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

Gotchas