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 KNN from Sorted Embeddings
00:00
5 left

Efficient KNN from Sorted Embeddings

HardPython

Problem

Givzey's embedding retrieval pipeline stores candidate embeddings sorted by their first coordinate. Given a sorted query embedding array queries and a sorted candidate embedding array candidates, return the indices of the k nearest candidates for every query using squared Euclidean distance.

For each query, use the ordering to avoid scanning candidates that cannot improve the current top-k results. Return one list per query, with candidate indices ordered by increasing distance. If distances tie, any ordering among tied candidates is valid.

Formal Specification

Implement k_nearest_neighbors(queries, candidates, k).

  • queries is a list of n embeddings, each represented by a list of d numbers.
  • candidates is a list of m embeddings, each represented by a list of d numbers.
  • candidates is sorted in nondecreasing order by embedding[0].
  • queries is also sorted by embedding[0], although each query must still be processed independently.
  • Return a list of n lists. Each inner list contains the indices of the k nearest candidates.
  • Use squared Euclidean distance, so square roots are unnecessary.

Constraints

  • 1 <= len(queries), len(candidates) <= 10^5
  • 1 <= embedding dimension <= 64
  • 1 <= k <= len(candidates)
  • All embeddings have the same dimension
  • Candidates are sorted by their first coordinate
  • All coordinates are finite numbers

Function Signature

def k_nearest_neighbors(queries, candidates, k):
Interviewer

Your question is Efficient KNN from Sorted 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.