RAG interview question · Most asked

Write a minimal retrieval-augmented generation pipeline: chunk, embed, retrieve and answer with citations.

Hands-on Asked in Native AI Engineer · Forward Deployed Engineer (FDE) interviews

Members can save this page, take notes and track progress. See membership · Sign in

Summary

Split documents into overlapping chunks with their source, embed them, store vectors, embed the question, take the top-k most similar chunks, and send them to the model with instructions to answer only from them and cite the chunk IDs. Even a small version should keep sources so answers can be checked.

Longer Answer

In production you'd add a vector database (pgvector, Qdrant, OpenSearch), hybrid search, reranking, metadata filters (tenant, permissions, dates), caching, and evaluation. Chunk size and overlap are tuned against retrieval metrics, not chosen once.

Example

import numpy as np
from anthropic import Anthropic
from sentence_transformers import SentenceTransformer

embedder = SentenceTransformer("all-MiniLM-L6-v2")
client = Anthropic()

def chunk(doc_id: str, text: str, size: int = 800, overlap: int = 150):
    step = size - overlap
    return [{"id": f"{doc_id}#{i}", "text": text[i:i + size]} for i in range(0, len(text), step)]

chunks = [c for doc_id, text in load_documents() for c in chunk(doc_id, text)]
vectors = embedder.encode([c["text"] for c in chunks], normalize_embeddings=True)

def answer(question: str, k: int = 5) -> str:
    q = embedder.encode([question], normalize_embeddings=True)[0]
    top = np.argsort(vectors @ q)[::-1][:k]
    context = "\n\n".join(f"[{chunks[i]['id']}] {chunks[i]['text']}" for i in top)
    msg = client.messages.create(
        model="claude-sonnet-5-5", max_tokens=600,
        system="Answer only from the context. Cite chunk ids in brackets. If the answer isn't there, say so.",
        messages=[{"role": "user", "content": f"Context:\n{context}\n\nQuestion: {question}"}])
    return msg.content[0].text

Key Points

  • Chunk with overlap and keep sources.
  • Embed chunks and query with the same model.
  • Top-k by cosine similarity.
  • Prompt: answer only from context, cite IDs.
  • Add filters, reranking and evals in production.

Explanation

What interviewers look for

A correct end-to-end sketch and knowing what production adds.

More RAG questions

← All RAG questions

Last reviewed · By the ByteCamel team · How we write content

Was this page helpful?

Report a mistake