Skip to content

Interview question · Build-an-Agent Coding Rounds

Implement a dispatcher that runs independent calls in parallel and dependent ones in order.

Reads that do not depend on each other run together; writes, and calls that need another call's result, wait their turn. Results come back keyed by call id, in the order the model asked, with timeouts that cannot hang the turn.

hardEssentialNewUpdated Nov 2026

TL;DR: Treat a turn's calls as a small dependency graph: a call depends on any earlier call it references, and writes to the same resource are ordered. Run each call as soon as its dependencies finish, with independent reads concurrent, each under a timeout. Collect results keyed by call id and return them in issue order, with failures and timeouts as structured results so one slow call cannot take the turn down. Test with fake tools that record start and end times.

How to approach it

Agree the call shape (id, tool, args, optional after list and a resource key for writes), choose asyncio for concurrency, implement scheduling by dependencies, then timeouts and ordering of results, then tests that prove overlap and order.

A strong answer

import asyncio

async def dispatch(calls, tools, timeout=1.0):
    done = {c["id"]: asyncio.Event() for c in calls}
    results, last_write = {}, {}
    deps = {}
    for c in calls:                                   # explicit + same-resource writes
        d = set(c.get("after", []))
        if c.get("writes"):
            if c["writes"] in last_write:
                d.add(last_write[c["writes"]])
            last_write[c["writes"]] = c["id"]
        deps[c["id"]] = d

    async def run_one(c):
        for d in deps[c["id"]]:
            await done[d].wait()
            if "error" in results[d]:
                results[c["id"]] = {"error": "dependency_failed", "dependency": d}
                done[c["id"]].set()
                return
        try:
            out = await asyncio.wait_for(tools[c["tool"]](**c["args"]), timeout)
            results[c["id"]] = {"ok": out}
        except asyncio.TimeoutError:
            results[c["id"]] = {"error": "timeout", "retryable": True}
        except Exception as e:
            results[c["id"]] = {"error": "tool_failed", "type": type(e).__name__}
        done[c["id"]].set()

    await asyncio.gather(*(run_one(c) for c in calls))
    return [(c["id"], results[c["id"]]) for c in calls]   # issue order

log = []
def tool(name, delay):
    async def fn(**args):
        log.append(("start", name))
        await asyncio.sleep(delay)
        log.append(("end", name))
        return name
    return fn

tools = {"a": tool("a", 0.05), "b": tool("b", 0.05), "w": tool("w", 0.01),
         "slow": tool("slow", 0.5)}
calls = [{"id": "1", "tool": "a", "args": {}}, {"id": "2", "tool": "b", "args": {}},
         {"id": "3", "tool": "w", "args": {}, "after": ["1"]},
         {"id": "4", "tool": "slow", "args": {}}]
out = asyncio.run(dispatch(calls, tools, timeout=0.2))
print(out[3])  # => ('4', {'error': 'timeout', 'retryable': True})
print(log.index(("start", "b")) < log.index(("end", "a")))  # => True
print(log.index(("start", "w")) > log.index(("end", "a")))  # => True

Reads a and b overlap; w waits for a because it declared the dependency; the slow call times out without holding up the others; results return in issue order with their ids.

What to test and say.

  • Overlap of independent calls (start of one before end of another).
  • Order for dependencies and same-resource writes.
  • A failed dependency produces dependency_failed rather than running the dependent call.
  • Timeouts produce a structured result; the turn completes.
  • "In production I would cap concurrency per tool and per downstream service, and refuse two writes to the same resource in one turn if the model decided both before seeing either result."

What interviewers probe next

"How do you know which calls depend on each other if the model does not say?" Treat writes as dependent on earlier calls in the turn and reads of the same resource after a write as dependent on it; or require the model's API to express dependencies.

"What about cancellation?" Pass a cancellation signal and cancel pending tasks; results for cancelled calls say so.

"Threads instead of asyncio?" Fine for blocking tools: a thread pool with futures and the same dependency logic.

What if?

What if · interview follow-ups

Hold your answer as the constraints change

Your dispatcher schedules calls by dependencies with timeouts and returns results in issue order.

  1. Start here

    What if the dependency list contains a cycle?

How it's scored

Weak: Runs everything sequentially, or everything in parallel including writes.

Hire: Parallel independent calls, ordered dependent ones, results keyed by id.

Strong Hire: Dependency graph including same-resource writes, per-call timeouts, dependency-failure propagation, issue-ordered results, tests proving overlap, order and timeouts, and production notes on concurrency limits, cycles and unknown write outcomes.

Common mistakes

  • Results returned in completion order without ids.
  • Writes racing each other.
  • One timeout blocking the whole turn.
  • Tests that cannot show calls actually overlapped.
You just read a free answer. Every topic opens its first 5 answers to anyone. Sign in and the count per topic goes to 6, and the site remembers where you stopped in each one.Google sign-in · free · no payment details
HOW DID IT GO?
0
READING SIGNED OUT

Signing in raises your free answers from 5 to 6 per topic, adds an applied lesson in every topic and easy practice tests, and the site starts remembering you: mastery per topic, bookmarks, and a next-focus recommendation. Free, no card.

Sign in free
UP NEXT ON YOUR JOURNEY
DISCUSSION · 0

Nobody has commented. Post a correction or another way to answer.