Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started
Efficient Vector Similarity Search
00:00
5 left

Efficient Vector Similarity Search

MediumPython

Problem

Implement a function to perform an efficient vector similarity search.

Use cosine similarity between a query vector and each candidate vector. Return the indices of the k most similar candidates in descending similarity order, breaking ties by ascending index. The function signature is def vector_similarity_search(query, vectors, k):; query is a nonzero numeric vector, vectors is a list of same-dimensional nonzero vectors, and the result is a list of indices.

Constraints

  • 1 <= len(vectors) <= 5000
  • 1 <= len(query) == len(vectors[i]) <= 100
  • 1 <= k <= len(vectors)
  • All vector components are finite numbers
  • The query and every candidate vector have nonzero Euclidean norm

Function Signature

def vector_similarity_search(query, vectors, k):
Interviewer

Your question is Efficient Vector Similarity Search. Start with the requirements in the Question tab.

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.
CodePython 3
You need to log in / sign up to run or submit.Ln 2
Run your code to see test output here.