AlgoViz

Coding with an AI assistant: spec → generate → verify

medium 14 min

An AI assistant writes code fast, but it stops when the code looks done, not when it is done.

Spec, generate, verify in 90 seconds

1:25Reel coming soon

Catch the bug the assistant missed

9:30Lesson coming soon

How it works

Step through the idea, or press Play.

Step 1 of 3

Spec. Inputs, outputs, edge cases and errors, written down before any code

Understand it

Whiteboard sketch: a loop of three stations. A clipboard checklist labelled Spec, a robot at a laptop labelled Generate, and a magnifying glass over a green tick and a red cross labelled Verify, with a 'fix' arrow back to Spec.
Spec → generate → verify, and back to the spec when verification finds a gap.

A coding assistant predicts plausible code. Plausible is not the same as correct. The output compiles, reads well and often passes the happy path, while an edge case or a security hole sits underneath.

The loop
  1. 1SpecSignature, edge cases, errors, constraints, examples
  2. 2Failing testsEncode the spec; check they fail for the right reason
  3. 3GenerateThe assistant writes the implementation
  4. 4VerifyRun the checks, read the diff, probe the edges
Repeat: back to Spec

When verification finds a gap, fix the spec or the tests first, then go round again.

Step 1

Write a spec the model cannot misread

  • Name the function, its inputs and their types, and what it returns.
  • List the edge cases: empty input, zero, negative numbers, the last page, duplicates, very large values.
  • Say what happens on bad input: raise which error, or return what.
  • State the constraints: no new libraries, follow the pattern in an existing file, keep the public API the same.
  • Give two or three concrete input → output examples. Examples remove more ambiguity than prose.
Text
Write chunk(items: list, size: int) -> list[list] in utils/lists.py.- Split items into consecutive lists of length size; the last may be shorter.- Empty items -> [].- size < 1 -> raise ValueError("size must be >= 1").- Do not modify items. No new dependencies. Follow the style of utils/strings.py.Examples:  chunk([1, 2, 3, 4, 5], 2) -> [[1, 2], [3, 4], [5]]  chunk([], 3)              -> []  chunk([1], 0)             -> ValueError
An example spec (a made-up function). Every line is something you can test.

Step 2

Generate, ideally test-first

Ask for failing tests that encode the spec, check that they fail for the right reason, then ask for the implementation.

Python
import pytestfrom utils.lists import chunkdef test_splits_with_short_last_chunk():    assert chunk([1, 2, 3, 4, 5], 2) == [[1, 2], [3, 4], [5]]def test_empty_input():    assert chunk([], 3) == []def test_rejects_size_below_one():    with pytest.raises(ValueError):        chunk([1], 0)def test_does_not_modify_input():    items = [1, 2, 3]    chunk(items, 2)    assert items == [1, 2, 3]
Tests written from the spec above, before any implementation exists.

Step 3

Verify

Give the assistant (and yourself) a check that returns pass or fail: a test suite, a type checker, a linter, a build. Then do the parts a test run cannot:

Do

  • Ask for evidence: the command that ran and its output.
  • Read the whole diff.
  • Probe the boundaries the tests skipped: off-by-one, empty input, None, unicode, time zones.
  • Get a second opinion from something that did not write the code: a colleague, or a fresh review session with no memory of the reasoning.

Don't

  • Accept a summary saying all tests pass.
  • Miss changed tests, deleted assertions, skipped tests and files you did not expect to change.
  • Trust that the happy path passing means the edges work.
  • Let the same session that wrote the code be its only reviewer.

Security needs its own pass

Python
cur.execute(f"SELECT * FROM users WHERE email = '{email}'")   # injectablecur.execute("SELECT * FROM users WHERE email = %s", (email,))  # parameterised
The first line is the kind of thing to catch in review; the second passes the value as a parameter.

AI-enabled interview rounds test this loop

In 2025 Meta confirmed it was piloting coding interviews where candidates can use an AI assistant. Public details on scoring are thin, so prepare for what such a round can see:

  • How you break down the problem.
  • How clearly you prompt.
  • Whether you test and question the output.
  • The quality of the final code.
  • How you explain your choices.

Key points

  • Plausible is not correct: you define what correct means and prove the code meets it.
  • A spec names the signature, edge cases, error behaviour, constraints and concrete examples.
  • Tests first, checked to fail for the right reason, then the implementation.
  • Verify with evidence, a full read of the diff, and your own edge probes; get a second opinion.
  • Give security its own pass and vet every new dependency by name.

Sources