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
ExperiencesCoaching
New
Practice
Mock InterviewsTimed interview simulations with feedbackSuccess 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
Get started
High-Dimensional Embedding Similarity
00:00
5 left

High-Dimensional Embedding Similarity

HardPython

Problem

Roblox may compare high-dimensional embeddings for experiences, avatars, or recommendations. Given query embeddings and candidate embeddings, return the exact k candidates with the highest cosine similarity for every query while avoiding storage of the full query-by-candidate similarity matrix.

Implement top_k_embedding_matches(query_embeddings, candidate_embeddings, k).

Formal Specification

  • query_embeddings is a non-empty list of Q vectors.
  • candidate_embeddings is a non-empty list of C vectors.
  • Every vector is a list of D finite numbers, and all vectors have the same dimension.
  • k satisfies 1 <= k <= C.
  • Return a list of Q lists. Each inner list contains exactly k pairs [candidate_index, similarity], ordered by decreasing cosine similarity and then increasing candidate index.
  • Return each similarity rounded to 6 decimal places.
  • The cosine similarity of any zero vector with another vector is defined as 0.0.

Your implementation must normalize each vector once, process candidates in blocks, and maintain only a bounded min-heap per query. Do not construct a Q x C matrix.

Constraints

  • 1 <= len(query_embeddings), len(candidate_embeddings) <= 10^4
  • 1 <= len(query_embeddings[i]) == len(candidate_embeddings[j]) <= 512
  • All vector values are finite integers or floating-point numbers
  • 1 <= k <= len(candidate_embeddings)
  • Zero-vector similarity is defined as 0.0
  • Results must be ordered by descending similarity, then ascending candidate index

Function Signature

def top_k_embedding_matches(query_embeddings, candidate_embeddings, k):
Interviewer

Whenever you're ready, walk me through your approach or go straight to the code. The requirements are in the Question tab.

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.