Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Top-k Similarity Search Algorithm

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Top-k Similarity Search Algorithm. Start with the requirements on the right.

Run and submit as often as you like. When you're ready, talk me through your approach or go straight to the code.

You need to log in / sign up to run or submit.

Problem

Bristol Myers Squibb's research knowledge systems can represent document chunks as pre-computed embedding vectors. Given these chunks, a query vector, and an integer k, return the IDs of the k most similar chunks using cosine similarity.

Formal Specification

Implement top_k_similarity_search(chunks, query_vector, k). chunks is a list of dictionaries, each containing a unique string id and an embedding list of numbers. query_vector is a numeric list with the same dimension as every embedding. Return a list of at most k IDs, ordered from highest cosine similarity to lowest.

If two chunks have the same similarity, place the chunk appearing earlier in chunks first. Use a bounded min-heap so the algorithm does not sort every chunk when k is small.

Example 1:

chunks = [{"id": "a", "embedding": [1, 0]}, {"id": "b", "embedding": [0, 1]}, {"id": "c", "embedding": [0.8, 0.2]}]
query_vector = [1, 0]
k = 2
Output: ["a", "c"]

a has cosine similarity 1.0, and c has a higher similarity than b.

Example 2:

chunks = [{"id": "x", "embedding": [1, 1]}, {"id": "y", "embedding": [-1, -1]}, {"id": "z", "embedding": [2, 0]}]
query_vector = [1, 1]
k = 5
Output: ["x", "z", "y"]

k may exceed the number of chunks, so return all available IDs.

Constraints

  • 0 <= len(chunks) <= 10^5
  • 1 <= len(query_vector) <= 512
  • Every embedding has the same dimension as query_vector
  • All vectors have nonzero magnitude
  • 1 <= k <= 10^5
  • Embedding values are finite numbers

Function Signature

def top_k_similarity_search(chunks, query_vector, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output