Implement Vector Top-k Retrieval with Cosine Similarity
On this page
Problem statement
Implement
top_k_cosine(query, docs, k)in NumPy.queryis an embedding vector of length D anddocsis an (N, D) matrix of document embeddings. Return the indices and cosine similarity scores of the k most similar documents, best first. It should be fast for N in the millions on one machine.
The retrieval step of every RAG system, in miniature. Interviewers use it to check vectorisation, numerical care and complexity, and as a lead-in to approximate search.
Example

docs = [[1,0,0], [0.9,0.1,0], [0,1,0], [0.7,0.7,0], [-1,0,0]]
query = [1, 0.2, 0]
top_k_cosine(query, docs, 3) → indices [1, 0, 3], scores [0.996, 0.981, 0.832]
Constraints (assumed)
- N up to a few million, D between 64 and 4,096, float32.
- k much smaller than N.
- NumPy only.
What this question tests
- Vectorising the similarity computation.
- Knowing cosine vs dot product and when they're the same.
- Selection in O(N) instead of a full O(N log N) sort.
- Thinking about scale: memory, batching queries, and when to use an ANN index.
Questions to ask first
- Are the embeddings already normalised? (Assume not.)
- One query or a batch? (One; batching is a follow-up.)
- Is k ever larger than N? (Clamp it.)
- Do we need filters (tenant, date)? (Not here.)
- Exact results, or is approximate acceptable? (Exact here.)
Try it yourself first
You have the full problem. Sketch your answer, then sign up free to read the complete walkthrough.
- The complete design, flow by flow, with pictures
- Deep dives into the hard parts
- What mid, senior and staff candidates are expected to say
- Full company question lists
Free forever. No card needed.