Apply model edits to a file with an AST and model routing
I was asked this
No reports yet. Sign in to say where you were asked it.
An AI system design interview question asked at Cursor. It tests code editing, model routing and speculative decoding.
An AI code editor lets a user ask for a change in plain language. A strong model answers with a sketch of the edit: the functions it changed, with comments such as # ... existing code ... standing in for code it left alone. Design the system that turns that sketch into an exact, reviewable edit of the file within about two seconds, and the router that decides which model handles each kind of request (tab completions, inline edits, chat, agent steps): a fast small one or a frontier one.
Requirements
- Functional: apply a sketch to the current file and show a diff the user can accept or reject; handle files the user edited while the model was answering; route each request type to a model; fall back when a model or provider is slow or failing.
- Non-functional: apply step in about two seconds for files of thousands of lines; tab completions in a few hundred milliseconds; the applied file must parse, and untouched code must stay byte-for-byte the same; cost per user kept within budget.
Clarifying questions
- Which languages must be supported, and is a parser available for each?
- How big are files, and how often does the sketch change code far apart in the file?
- What latency does each request type need (completion, inline edit, chat, agent step)?
- Can we fine-tune our own small model, or only call providers?
- What is the cost budget per user per day?
What a strong answer covers
- Two apply strategies: structural (parse the file and the sketch into syntax trees, match changed functions and classes by name and signature, splice them in, keep everything else) and a model rewrite (a fast model trained to merge the sketch into the full file, sped up with speculative decoding using the original file as the draft). Use the structural path when the match is unambiguous, the model path otherwise.
- Validation: the result parses, the diff touches only regions the sketch covered, placeholders never leak into code, and a failed check falls back to the other strategy or asks the strong model for a precise diff.
- Concurrency with the user: apply against the file version the model saw, then rebase onto the user's newer edits, or mark conflicts.
- Routing: a policy per request type (small fast models for completions, frontier models for planning and hard edits), escalation when the small model's answer fails validation, and per-user budgets.
- Numbers: tokens per apply (the whole file is rewritten in the model path), tokens per second needed to finish in two seconds, cost per request type per day.
- Fallbacks: health checks per provider, timeouts and hedged requests, switching to a second provider with the same prompt contract, and graceful degradation (a slower model, or a plain diff view).
Follow-ups
- The sketch changes a function that appears twice (overloads, same name in two classes). How do you place it?
- A provider's latency triples for an hour. What do users see?
- Measure apply quality in production: what counts as a failed apply?
- Edits span several files in one agent step. How do you apply and review them together?
Where it's asked
What it tests
code editing · model routing · speculative decoding · ast
Sign in to see the worked answer
The full answer, chapter by chapter, with what interviewers expect at each level. New accounts get 5 credits: each one unlocks a question for good.
What the labs asked this week
A weekly email of what OpenAI, Anthropic and the other top AI labs asked engineers, round by round.
