What they’re really asking
If you only remember one thing, say this in the first minute: “The repository is far bigger than what the model can read, so this is a small search engine with a hard size limit. I split the code into whole functions and classes, score them with a word search that understands identifiers plus a boost for code the query’s names point to, merge the rankings, and add chunks by value per token until the budget is full. Then I prove it works on a labelled set of questions, and I’m honest about how many questions it takes to tell two methods apart.”
The rest of this page explains each part of that answer.
The task: given a question and a repository, write the code that returns the pieces of code worth sending to a large language model (LLM). It sounds like a search feature you’ve built before, and mostly it is. Three things make it different.
- The output has a hard size limit. A model reads text in small pieces called tokens; code comes out at about 8 tokens per line. The most it can read per request is its context window, and the provider charges for every token you send. So you don’t return “the top 10 results”; you return as much useful code as fits in a budget, say 8,000 tokens, and nothing else.
- Code is full of exact names. A question about “retry backoff” should find
retry_with_backoff. Ordinary text search splits on spaces and would miss that. Names, imports and who-calls-what carry more meaning in code than in prose. - You must show it works. Anyone can return some code. The interviewer wants to see how you would know the right code made it in.
This problem is an example from a prep article about Cursor’s interview loop, which words it as “given a query and a repo, return the chunks worth sending to a model”; the article cites no candidates, so treat it as a likely shape (techinterview.org). The same article says graders look at whether you “prompt with the right context”.
Clarify first
Ask, and state an assumption for each:
- What is the query? A natural-language question in a chat panel, or the cursor position for completion? “I’ll assume a chat question.”
- Repository size and languages? “I’ll assume tens of thousands of lines of Python.”
- Token budget? “I’ll assume 8,000 tokens for context.”
- Can code be sent to an embedding service, or must everything stay local? “I’ll build the local word search first either way.”
- How will we judge it? “I’d like to write a small labelled set of questions now, so we can measure.”
The example we follow
A developer asks: “Where do we retry failed payment charges, and how is the backoff computed?” The repository has
a payments/client.py with a charge() method, a refunds/client.py with a private _retry() helper, and a
shared http/session.py. The names are illustrative. We want _retry() and the code charge() calls to end up in
the 8,000 tokens.
Answer
Words you need first
- Chunk: one unit you rank and return. Here, one function or class, with its file path and line numbers.
- BM25: the standard relevance score behind most text search engines. It rewards chunks that contain the query’s rarer words. Searching with it is called lexical search (search by words).
- Embeddings: each chunk turned into a list of numbers so that chunks with similar meaning sit close together. Searching with them is dense search, or search by meaning.
- Symbol graph: where each function, class and variable is defined, who calls it, and what each file imports. You get it from the language’s parser.
Step 1: chunk by structure
Parse each file and make one chunk per top-level function or class, plus the module docstring, with path and line span. Fixed windows of N lines cut functions in half, and half a function is little use to the model.
Step 2: score with several signals
- Lexical search over split identifiers. Before indexing, split names into words:
parseToolCallbecomesparse tool call,retry_with_backoffbecomesretry with backoff. Then score with BM25. In our example, “retry” and “backoff” now hit_retry()and its docstring. - Dense search, optionally. It helps when the question’s words don’t appear in the code (“where do we try again?”).
- Structural boosts. Raise chunks that define a name mentioned in the query or in the open file, and files the
open file imports. In our example,
charge()callsHttpSession.post(), so that definition gets a boost. - Merge the rankings with reciprocal rank fusion (RRF): each chunk scores
Σ 1 ÷ (60 + rank)over the lists it appears in, so a chunk near the top of several lists wins. It needs no tuning, which is why it is the usual first choice.
Step 3: pack under the budget
Estimate each chunk’s size at about 8 tokens per line (measured on 2026-10-12 over this site’s lab code with modern tokenizers). Remove chunks that overlap ones already chosen. Add chunks greedily by score per token, so a short, relevant chunk beats a long, slightly more relevant one. Stop at the budget. Return path, lines and text, so the model can say where its answer came from.
Here is how our example might pack (illustrative values):
| Candidate | Found by | Tokens | Kept? | What to say in the interview |
|---|---|---|---|---|
refunds/client.py: _retry() | lexical (“retry”, “backoff”) | 600 | yes | ”Identifier splitting is what found this.” |
payments/client.py: charge() | lexical and dense | 900 | yes | ”Ranked high in two lists, so RRF ranks it near the top.” |
http/session.py: HttpSession.post() | symbol graph | 700 | yes | ”Called by a top result, so it gets a boost.” |
| A long design doc about payments | dense | 6,000 | no | ”Low score per token: it would eat the budget.” |
A second copy of charge() from a vendored folder | lexical | 900 | no | ”Duplicate; removed.” |
Say: “I rank by several signals, pack by value per token, and stop at the budget. Then I measure.”
Step 4: measure, honestly
A small run shows what measuring looks like and what it can’t tell you. On 2026-10-12, 14 hand-written questions were run over this site’s 98 lab Python files, split into 532 function and class chunks. The target file was in the top 5 for 12 of 14 questions with BM25 and with dense search (a small embedding model, MiniLM), and 13 of 14 with the hybrid. The target file came first for between 6 and 10 of the 14, depending on the method.
Fourteen questions can’t separate those methods: one question is 7 percentage points. Saying so is part of a good answer.
The rag lab measured the same kind of comparison at a size that can. On BEIR SciFact (a public set of scientific claims with labelled relevant documents), hit@3, the share of queries with a relevant document in the top 3, was 69.3 / 68.3 / 72.7 % (measured on Apple M1 Ultra, 64 GB RAM, torch 2.14.1, sentence-transformers 6.1.0 (MPS), 2026-10-11 · rag lab) for dense, hybrid and reranked retrieval (a reranker is a second, slower model that re-scores the top few results). Switching the hybrid’s BM25 tokenizer from splitting on spaces to splitting on word characters gave 73.7 % (measured on Apple M1 Ultra, 64 GB RAM, torch 2.14.1, sentence-transformers 6.1.0 (MPS), 2026-10-11 · rag lab). How you split text into words changes what you find. That is the identifier-splitting lesson for code.
In production you don’t need hand labels. Label each request with the files the user touched next, and measure context recall: the share of those files your packer included at the budget. Compare two packers on the same requests.
The arithmetic behind the numbers (for a Staff-level follow-up)
- One question out of 14: 1 ÷ 14 ≈ 7 percentage points, so “12 of 14” versus “13 of 14” is a single question.
- RRF example: a chunk ranked 1st in lexical and 3rd in dense scores 1 ÷ 61 + 1 ÷ 63 ≈ 0.0164 + 0.0159 = 0.0323. A chunk ranked 1st in only one list scores 0.0164. Appearing high in two lists wins.
- Budget check for the example: 600 + 900 + 700 = 2,200 tokens kept so far; the 6,000-token design doc would push the total to 8,200, past the 8,000 budget, and its score per token is low anyway.
Senior vs Staff
Repo context retrieval
Senior
- Chunks by syntax, combines lexical and structural signals, packs to a budget and tests on labelled queries.
Staff
- Defines the evaluation from usage logs (files touched next), knows how many queries a claimed improvement needs, and decides when embeddings are worth their cost and privacy footprint.
In plain words: a Senior answer builds a good retriever and tests it. A Staff answer also designs how quality is measured from real usage, knows when a result is too small to trust, and decides whether embeddings earn their cost, including that they mean sending code to one more service.
Small-team version
You won’t build this for an IDE; you’ll buy the assistant. You will build it for internal tools, such as a bot that answers questions about your services or edits config, and the same recipe applies. Start with word search plus your symbol graph. Add a vector store only after your labelled set shows word search misses too much. The retriever is code you write; the model is an API you buy.
The cost is small. A packed 8,000-token context costs about $0.016 per call on Claude Sonnet 5 and about $0.002 on gpt-5-mini (list prices fetched 2026-10-11). So 1,000 internal questions a month cost $2–$16 plus output. Stuffing a 100,000-token context instead would cost about $0.20 per call on Sonnet 5, more than ten times as much, and usually answers worse. Add embeddings when most questions are in natural language and word-search recall on your labelled set stays low.
The arithmetic behind the costs
- Sonnet 5 input at $2 per million tokens: 8,000 × $2 ÷ 1,000,000 = $0.016. gpt-5-mini at $0.25 per million: 8,000 × $0.25 ÷ 1,000,000 = $0.002.
- 1,000 calls: $2 (gpt-5-mini) to $16 (Sonnet 5) a month, before output tokens.
- 100,000 tokens on Sonnet 5: 100,000 × $2 ÷ 1,000,000 = $0.20.
Pitfalls
- Fixed-size windows. They split functions in half, so the model gets code it can’t use.
- Whitespace tokenization. It misses the parts of
camelCaseandsnake_casenames, which is where code’s meaning lives. - No budget. Context grows until latency and cost do, and irrelevant code distracts the model.
- Claiming a winner from a dozen queries. One query moves the score by several points, so small sets can’t rank methods.
Go deeper
- Family walkthrough: Design an AI coding assistant, “Context selection under a token budget”.
- Labs: Build Your Own Search EngineNeeds measurement for BM25 and postings; Build Your Own RAG System for measured hybrid retrieval and its tokenizer ablation.