---
title: "Without a vector database, implement top-k retrieval: cosine similarity over an embedding matrix, a simple BM25 scorer…"
url: https://gurkanozkan.ai/learn/questions/applied-llm-cod-03/
markdown: https://gurkanozkan.ai/learn/questions/applied-llm-cod-03.md
kind: question
role: Applied LLM
stage: Coding
difficulty: Easy
json: https://gurkanozkan.ai/api/learn/questions/applied-llm-cod-03.json
robots: noindex
---

# Without a vector database, implement top-k retrieval: cosine similarity over an embedding matrix, a simple BM25 scorer, and reciprocal rank fusion of the two lists. Then discuss complexity and scaling.

Applied LLM · Coding · Easy

Theme reported publicly: [Cohere](https://www.techinterview.org/post/3233474931/cohere-interview-process-2026/)

## Answer

*Model answer, written in a candidate's voice*

### In short

- Normalise embeddings once, score with one matrix multiply, use argpartition then sort for top-k
- BM25: tokenise, document frequencies, IDF, tf saturation with k1, length normalisation with b and avgdl
- RRF: fuse ranked lists with 1/(60+rank), 1-based ranks, deterministic tie-break, dedupe by doc id
- Complexity: exact cosine O(N*d) per query, BM25 query O(query terms x postings), build/storage costs
- Scaling: ANN indexes, inverted indexes, batching, sharding, caching, quantisation, incremental updates

### In full

The core approach is to keep embeddings row-normalised, so cosine similarity becomes a single matrix multiply against a normalised query vector, then use argpartition to get the top-k candidates and sort only those k rather than the whole matrix. For BM25 I would tokenise consistently, lowercase and split on non-alphanumerics, optionally stem or lemmatise only if the corpus and evaluation justify it, and store document length, term frequencies, and document frequencies; the score is sum over query terms of IDF times tf*(k1+1)/(tf + k1*(1-b+b*dl/avgdl)), with IDF computed from corpus-wide df and k1 and b tuned on validation data. Reciprocal rank fusion then combines the dense and sparse lists by adding 1/(60+rank) for each list, using 1-based ranks, deduplicating by document id while keeping the merged contribution, and breaking ties deterministically, for example by original score then document id, so results are stable. Exact cosine is O(N*d) per query and O(N*d) memory, which is fine for small or medium corpora but not for very large ones; BM25 is cheap to query with an inverted index but needs careful df and length statistics. At scale I would move dense retrieval to an ANN index such as HNSW or IVF with quantisation, keep BM25 on an inverted index, batch queries, cache normalised embeddings and IDF, shard by tenant or document range, and accept approximate recall in exchange for latency, while still validating that the ANN and BM25 settings preserve the ranking quality the RRF fusion depends on.

*Corrected by an independent AI reviewer.*

## What a strong answer covers

- Normalised vectors, one matrix multiply, argpartition for top-k
- BM25 with IDF, k1, b and document-length normalisation; tokenisation choices
- RRF with k≈60; ties handled; deduplication
- Scaling: ANN indexes, inverted indexes, batching, sharding

## Common mistakes

- Forgetting normalisation
- full sort instead of partial selection

## Topic guides

- [Coding for ML and LLM engineering](https://gurkanozkan.ai/learn/topics/coding-engineering.md)
- [Retrieval-augmented generation](https://gurkanozkan.ai/learn/topics/rag.md)
---

*Study material compiled with AI: drafted by a local model (Inkling 2 Mini) from public sources, then checked against those sources by independent AI reviewers (Claude) in November 2026. Not reviewed line by line by a person.*
