Dataford
Interview QuestionsInterview GuidesExperiencesCoaching
New
Pricing
Get started
Dataford
Popular roles
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Browse
Browse All RolesEvery role hub, from analyst to MLBrowse All CompaniesCompany-specific interview loopsAll Interview GuidesThe full guide library
Top questions by role
Software EngineerData AnalystData ScientistData EngineerBusiness AnalystAI EngineerMachine Learning EngineerProduct Manager
Top questions by skill
SQLPythonStatisticsMachine LearningA/B TestingSystem DesignGenerative AIProduct SenseMetricsBehavioral
Browse all questions →Try a mock interview
Experiences
Practice
Mock InterviewsTimed interview simulations with feedbackCoaching1:1 coaching with people at your target companiesSuccess PathYour 6-week structured planModulesCurated lessons by topicWebinarsTalks from ex-Big Tech data leadsPlaygroundA free-form scratch editor
Learn
BlogInterview strategy and career adviceTech Job Market ReportHiring trends across data and AI rolesFor UniversitiesDataford for career centersAbout DatafordWho we are and how we build
Pricing
Build my plan
Top-k Similarity Search Algorithm
00:00
5 left

Top-k Similarity Search Algorithm

MediumPython

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):
Interviewer

Your question is Top-k Similarity Search Algorithm. 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
Sign up free to run your codeLog inLn 2
Run your code to see test output here.