SDE & MLE Prep

Retrieval-Augmented Generation (RAG)

Q: Design a RAG system over company documents.

INDEXING:  docs -> parse/clean -> chunk (300-800 tokens, overlap) -> embed -> vector DB (+ BM25 index, metadata)

QUERY:     question -> (rewrite / expand) -> hybrid retrieve top-50 (dense + BM25)
                    -> rerank (cross-encoder) -> top-5 chunks
                    -> prompt: system + context chunks + question -> LLM -> answer with citations

Design points:

  • Chunking respects structure (headings, tables); store metadata for filtering and ACLs.
  • Hybrid search (embeddings + keyword) is more robust than either alone; rerankers boost precision.
  • Ground answers in the context, ask for citations, allow "I don't know".
  • Evaluate: retrieval recall@k / MRR on a labeled set, answer faithfulness and relevance (LLM-as-judge + human spot checks), latency and cost.
  • Failure modes: wrong chunk granularity, stale index, missing permissions filtering, context overflow, prompt injection via retrieved docs.