Prompt & Context Engineering·Question 27 of 68Intermediate

Design an example store for dynamic few-shot selection. What do you get, and what does it cost you?

Type it the way you would explain it to an interviewer. A few sentences is enough; you can't break anything.

Model answer: the part to lead with

You embed a curated set of labelled examples, retrieve the top-k most similar to each incoming input at request time, and render them into the prompt. Compared with a fixed set of five examples, the model sees demonstrations that are actually relevant to this input.

Full worked answer with diagram

You embed a curated set of labelled examples, retrieve the top-k most similar to each incoming input at request time, and render them into the prompt. Compared with a fixed set of five examples, the model sees demonstrations that are actually relevant to this input.

def select(query: str, k: int = 5) -> list[Example]:
    cands = index.search(embed(query), top_n=50)
    picks = mmr(cands, k=k, lambda_=0.6)   # relevance vs diversity
    return balance_labels(picks)           # avoid all-one-label prompts

Three design decisions matter. Diversity: plain top-k returns five near-duplicates, which teaches the model one narrow pattern; MMR or clustering fixes it. Label balance: models exhibit majority-label bias, so five examples that happen to share a label bias the prediction toward it regardless of the input. Cap: returns diminish fast, and a big example block competes for attention with the actual task.

Where it earns its keep: large or subjective label taxonomies, per-tenant conventions where each customer's "correct" differs, and domains where the boundary is genuinely example-shaped.

The cost people forget is caching. Static examples sit in your cacheable prefix. Dynamic ones vary per request, so they either sit after the static block (shortening your cached prefix to system-plus-tools only) or, worse, get placed above it and destroy the cache entirely. A good middle path is bucketing: cluster requests into a handful of coarse intents, and use a fixed example set per intent. You keep most of the relevance win and every bucket stays cacheable.

The part that is a real engineering commitment: the store is a dataset. It drifts, it needs curation, and a single mislabelled example is a bug that silently teaches wrong behaviour to every similar request forever. Version it, review additions, and feed it from reviewed production failures.

And measure it. Dynamic few-shot has to beat static few-shot, which has to beat zero-shot, on your eval. On instruction-tuned models, often it does not, and you have built infrastructure for nothing.

Worth sketching. Two stages between retrieval and rendering are where the quality lives, and the curation lane shows you treat the store as a dataset.

flowchart LR
    A["Incoming input"] --> B["Embed"]
    B --> C["Top 50 by similarity"]
    C --> D["MMR: relevance<br/>versus diversity"]
    D --> E["Balance labels"]
    E --> F["Render k examples<br/>after the cached prefix"]
    G["Reviewed production<br/>failures"] --> H["Curated, versioned store"]
    H --> C

What a good answer covers

  • You embed a curated set of labelled examples, retrieve the top-k most similar to each incoming input at…
  • Compared with a fixed set of five examples, the model sees demonstrations that are actually relevant to…
  • Diversity: plain top-k returns five near-duplicates, which teaches the model one narrow pattern; MMR or…
  • Label balance: models exhibit majority-label bias, so five examples that happen to share a label bias…
  • Cap: returns diminish fast, and a big example block competes for attention with the actual task

Key terms: Diversity · Label balance · Cap · caching · the store is a dataset

Interviewer follow-ups to expect

  1. How would you keep a mislabelled example from silently degrading production?

  2. When would bucketed static examples beat true per-request retrieval?

Worked answer from AI-Engineer-Interview-Questions by Om (MIT), reformatted for LLM Prep · answer text last updated 6 November 2026 · scoring and review schedule by LLM Prep