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
Complete KNN Methods
00:00
5 left

Complete KNN Methods

HardPython

Problem

Cohere Health uses patient feature vectors to support risk classification workflows. Implement K-nearest neighbors classification for a batch of query vectors without relying on machine-learning libraries.

Given labeled training vectors, return one predicted label for each query vector. Use Euclidean distance and select the k closest training points. When weighted is false, each neighbor contributes one vote. When it is true, each neighbor contributes 1 / distance weight. If a query exactly matches one or more training vectors, use only those zero-distance points for voting. Break every voting tie by choosing the lexicographically smallest label.

Formal Specification

Implement knn_predict(train_X, train_y, query_X, k, weighted). train_X and query_X are lists of equal-dimensional numeric vectors. train_y contains one non-empty string label for each training vector. Return a list of strings, one per query vector. The input must not be mutated.

Use a bounded max-heap so neighbor selection requires O(n log k) time per query rather than sorting every distance.

Constraints

  • 1 <= len(train_X) <= 100000
  • 1 <= len(query_X) <= 1000
  • 1 <= k <= len(train_X)
  • 1 <= len(train_X[0]) <= 50
  • All vectors have the same dimension
  • All feature values are finite numbers
  • train_y contains one non-empty string per training vector

Function Signature

def knn_predict(train_X, train_y, query_X, k, weighted):
Interviewer

Your question is Complete KNN Methods. 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.