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
Efficient k-NN for Embeddings
00:00
5 left

Efficient k-NN for Embeddings

HardPython

Problem

Arrowstreet Capital's research systems compare high-dimensional embeddings repeatedly. Implement an exact k-nearest neighbors search that avoids sorting every distance for each query by indexing the embeddings in a vantage-point tree.

Formal Specification

Write knn_search(embeddings, query, k). embeddings is a non-empty list of equal-length numeric vectors, and query is a vector with the same dimension. Use squared Euclidean distance. Return the indices of the k closest embeddings, ordered by increasing distance. Break equal-distance ties by increasing original index.

The function may construct the VP-tree during the call. The returned result must be exact, not approximate. A VP-tree recursively selects a pivot, partitions points by their distance from that pivot, and uses triangle-inequality bounds to prune subtrees during search. Maintain only the best k candidates while traversing.

Examples

Example 1

Input: embeddings = [[0, 0], [2, 0], [0, 3], [-1, 0], [5, 5]], query = [0, 0], k = 3
Output: [0, 3, 1]

Distances are 0, 1, 4, 9, and 50, respectively.

Example 2

Input: embeddings = [[1, 1], [1, 1], [2, 2]], query = [1, 1], k = 2
Output: [0, 1]

The first two vectors are tied at distance zero, so their original indices determine the order.

Constraints

  • 1 <= len(embeddings) <= 100,000
  • 2 <= len(embeddings[0]) <= 512
  • 1 <= k <= len(embeddings)
  • All vectors have equal dimension and finite numeric coordinates
  • Use only Python's standard library

Constraints

  • 1 <= len(embeddings) <= 100,000
  • 2 <= len(embeddings[0]) <= 512
  • 1 <= k <= len(embeddings)
  • All embeddings have the same dimension as query
  • All coordinates are finite numeric values

Function Signature

def knn_search(embeddings, query, k):
Interviewer

Your question is Efficient k-NN for Embeddings. 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.