RAG Retrieval: Bi-Encoders vs Cross-Encoders, Similarity Metrics and ANN Vector Search
Company: ByteDance
Role: Machine Learning Engineer
Category: Machine Learning
Difficulty: hard
Interview Round: Technical Screen
Explain how the retrieval module of a retrieval-augmented generation (RAG) system works, then go deep on the embedding search behind it: the encoder architectures used to judge relevance, the similarity measures, and how vector search scales to large corpora.
### Clarifying Questions
- How large is the corpus (thousands, millions or billions of chunks), and how often does it change?
- What latency budget does retrieval have within the end-to-end response time?
- Do queries need metadata filters such as access permissions, language or date?
- Is the embedding model used off the shelf, or can it be fine-tuned on in-domain query-document pairs?
### Part 1 — The retrieval module
Describe the retrieval module end to end: what happens offline to the documents, and what happens online for each query until the retrieved text reaches the generator.
```hint Two paths
Split the module into an indexing path and a query path, and ask which decision made at indexing time limits what the query path can ever return.
```
#### What This Part Should Cover
- Chunking, embedding and indexing, with the metadata kept alongside
- Query encoding, top-k search, reranking and prompt assembly
- Measuring retrieval quality separately from answer quality
### Part 2 — Bi-encoders versus cross-encoders
Compare bi-encoders and cross-encoders: how each computes relevance, their accuracy and their cost, and why systems commonly use a bi-encoder for recall followed by a cross-encoder to rerank.
```hint What can be precomputed
Ask which of the two architectures lets you compute a document's representation before any query arrives, and what that costs in accuracy.
```
#### What This Part Should Cover
- How each architecture scores a query-document pair
- Cost per query as a function of corpus size
- The two-stage design and how the candidate count trades recall against reranking cost
### Part 3 — Embeddings and similarity measures
Explain what a text embedding is, and compare cosine similarity, dot product and Euclidean (L2) distance. When do they produce the same ranking, and how do you choose among them?
```hint Vector length
Consider what each measure does with the length of the vectors, and what happens to all three when every vector is normalized.
```
#### What This Part Should Cover
- Definitions of the three measures and how vector magnitude affects each
- The exact relationship between them for normalized vectors
- Matching the measure to how the embedding model was trained
### Part 4 — Exact versus approximate vector search
What does exact (brute-force) vector search cost per query, and how do approximate nearest neighbor methods such as HNSW, IVF and product quantization reduce that cost? Sketch how a vector database is built on top of them.
```hint Two separate costs
Brute-force cost has two factors: how many vectors each query is compared with and how expensive each comparison is. Ask which method attacks which factor.
```
#### Clarifying Questions for this Part
- What recall, relative to exact search, is acceptable, and must the whole index fit in memory?
#### What This Part Should Cover
- Brute-force time and memory per query, with an estimate
- How HNSW, IVF and product quantization work, their tuning knobs and their recall, latency and memory trade-offs
- A vector database's write path, deletes, metadata filtering, sharding and quality monitoring
### What a Strong Answer Covers
- An end-to-end pipeline with indexing and query paths and a clear place for reranking
- Correct reasoning about what can be precomputed and what each stage costs
- The exact relationship between the similarity measures and a rule for choosing one
- Quantified brute-force cost and precise mechanics of graph, partition and quantization indexes
- How retrieval quality is measured and kept up as the corpus and model change
### Follow-up Questions
- Retrieval returns relevant-looking chunks but the answer is still wrong. How do you tell whether retrieval or generation is at fault?
- How would you enforce a filter such as "only documents this user may see" without ruining recall or latency?
- Users search for exact product codes and rare names that embeddings handle poorly. What would you add?
- The corpus doubles and new documents arrive continuously. How do you keep the index fresh without rebuilding it from scratch?
Overview: A retrieval question for machine learning engineers: explain how the retrieval module of a RAG system works, compare bi-encoders with cross-encoders and cosine, dot-product and L2 similarity, analyze brute-force vector search cost, and describe HNSW, IVF, product quantization and how a vector database is built.