CODING CHALLENGE · N°17

Run the Goal Loop

Medium AI EngineeringAgentsLoop Engineering

The goal loop is the engine of loop engineering: attempt the next task, let a verifier judge it, retry failures, and stop when the goal is met — or stop loudly when the cap trips. Implement the whole outer loop as one pure, testable function.

The problem

Implement run_loop(queue, outcomes, max_iterations). Each iteration the agent attempts the task at the front of the queue, and outcomes[i] is the independent verifier’s verdict for iteration i (if the outcomes list is exhausted, the verdict is a failure). A pass moves the task to done; a fail leaves it at the front to retry. The loop ends when the queue is empty or the iteration cap is hit. Return {"done": […], "remaining": […], "iterations": n, "status": "complete" | "cap"}.

EXAMPLE 1
Input queue=["a","b"], outcomes=[True, False, True], max_iterations=10
Output {'done': ['a', 'b'], 'remaining': [], 'iterations': 3, 'status': 'complete'}
"b" fails once and is retried — the verifier decides, not the agent
EXAMPLE 2
Input queue=["a","b"], outcomes=[False, False, False], max_iterations=3
Output {'done': [], 'remaining': ['a', 'b'], 'iterations': 3, 'status': 'cap'}
the cap trips with work remaining — the loop ends loudly, not silently
EXAMPLE 3
Input queue=[], outcomes=[True], max_iterations=5
Output {'done': [], 'remaining': [], 'iterations': 0, 'status': 'complete'}
nothing to do → the goal is already met, zero spend
CONSTRAINTS
  • One verifier verdict is consumed per iteration: verdict for iteration i is outcomes[i], or a failure once outcomes are exhausted.
  • A failed task stays at the front of the queue — state untouched, retried next iteration.
  • Do not mutate the input queue; return a fresh remaining list.
  • status is "complete" only when the queue emptied; a cap-hit with leftovers is "cap" — even at 0 iterations.
SOLVE IT YOURSELF

Your turn — write it

Edit the stub, hit Run (or ⌘/Ctrl + Enter), and watch the hidden tests. Stuck? the hints are right above and Reveal solution is one click away.

YOUR TASK

Implement run_loop(queue, outcomes, max_iterations) — attempt the front task each iteration, let the verifier verdict decide pass/retry, and stop on goal-met or cap-hit with the full result dict.

HINTS — 5 IDEAS
  1. Copy the queue first — the caller’s list must survive the run.
  2. Loop while the queue is non-empty AND iterations < max_iterations.
  3. The verdict for this iteration is outcomes[iterations] if that index exists, else False — read it before incrementing.
  4. On a pass, pop the front task into done. On a fail, do nothing — the front task is automatically the retry target.
  5. Status at the end: "complete" if the queue is empty, otherwise "cap".
CPython · WebAssembly

Explore the topic

See this challenge alongside everything else on the same subject — handbooks, system designs, algorithms and tools, in one place.