Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
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

Your question is High-Dimensional Embedding Similarity. 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.