KatInterview

Designing a Document Q&A Retrieval Backend with Pluggable Grading

System DesignModel answer
MediumModel answerSystem Designragvector-databasesolid

Last updated

Design a RAG ingestion pipeline, a swappable response-grading component using dependency inversion, and an efficient top-k structure for ranking retrieved chunks.

Backend for a Document Question-Answering Service

Your team is building the backend for a service that answers questions over a large collection of uploaded PDF files. Uploaded documents are split into pieces, stored in a searchable form, and at question time the most relevant pieces are passed to a Large Language Model (LLM) so it can write a grounded answer. This pattern is commonly called Retrieval-Augmented Generation (RAG).

Answer the three parts below as you would in a technical discussion. Be concrete, state trade-offs, and tie each answer to the needs of the system.

Part 1: Ingestion and indexing

Describe an extract-transform-load (ETL) pipeline that turns uploaded PDFs into something the retrieval layer can query.

  1. How would you split documents into chunks so that each chunk still carries enough meaning to be useful on its own?
  2. What do the embedding model and the vector database each contribute, and how do they interact at ingestion time and at query time?

Part 2: Pluggable response grading

You must add an "evaluator" component that scores how good the LLM's answers are. Different teams want different grading approaches, for example one based on embedding similarity to a reference answer and another based on keyword overlap.

How would you apply the Dependency Inversion Principle so that new grading strategies can be added without changing the main retrieval/answering service? Sketch the abstractions and show how the pieces are wired together.

Part 3: Keeping the best results

While searching, the system keeps the k highest-scoring chunks seen so far.

  1. If these are held in a sorted linked list, what is the time complexity of inserting one new candidate?
  2. Which data structure would you use instead, and why? Give the cost of processing N candidates, and mention edge cases.

Hints

Hint 1

For chunking, think about what gets lost when text is cut at arbitrary character counts, and what you can add (structure, overlap, headings) to recover it.

Hint 2

For the evaluator, ask which module should own the interface: the high-level service that consumes scores, or the individual graders. Then ask who decides which concrete grader is created.

Hint 3

For top-k, you only ever need to know the weakest item currently kept. Which structure exposes its minimum cheaply and supports fast replacement?