KatInterview

Building a Citation-Accurate Document Q&A Service over PDFs

System DesignModel answer
MediumModel answerSystem Designragdocument-parsingvector-search

Last updated

Design a service that ingests text, scanned and table-heavy PDFs, answers questions with page-level citations, and keeps its index correct as documents are replaced or removed.

Design a Citation-Accurate Q&A Service over PDFs

Your team wants an internal service where users upload PDFs and then ask natural-language questions. Answers must be generated by an LLM from the uploaded content, and every claim must point back to the exact page it came from.

What the system must do

  1. Ingest PDFs that contain a mix of selectable text, scanned pages needing OCR, and tables.
  2. Answer questions with context drawn from the documents and explicit page-level citations.
  3. Handle changes: users can replace or delete a document, and the search index must always reflect the current version only.

Quality and performance goals

  • Faithfulness: keep hallucination low, and make sure each citation really matches the text that supports the claim.
  • Latency: ingestion may be asynchronous. Retrieval (before LLM generation) should finish in well under a second.
  • Scale: thousands of documents, with many concurrent readers.

Sizing assumptions

  • About 5,000 PDFs, averaging 50 pages each.
  • Read traffic is high and concurrent; writes are comparatively rare.

What your answer should cover

  • Parsing: how you deal with multi-column pages, tables, and scans.
  • Indexing: the data model for chunks and their metadata (document ID, page, and so on).
  • Citation correctness: how you ensure the page numbers shown to the user are right.
  • Lifecycle: how you version documents and remove stale chunks without serving mixed or deleted content.

Include a rough capacity estimate, the main components, and the trade-offs behind your choices.

Hints

Hint 1

Start with arithmetic: pages, chunks per page, embedding dimension and bytes per vector. The result tells you whether you need sharding or only read replicas.

Hint 2

Do not let the LLM write page numbers. Give each retrieved chunk an opaque tag, have the model cite tags, and resolve tags to pages in your own code, then validate.

Hint 3

Treat each document version as an atomic unit: build the new version invisibly, then switch the active pointer in one step, and clean up the old chunks afterwards.

Hint 4

For layout, think in stages: classify the page, extract text or OCR, detect regions, recover reading order, then build structure-aware chunks that never silently cross page boundaries.