Dataford
Interview QuestionsInterview GuidesExperiencesMock InterviewsPricing
Get started

Implementing a Classification Algorithm

MediumPython00:00
Practice interviewer
In session
5 left
00:00

Your question is Implementing a Classification Algorithm. Start with the requirements on the right.

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.

Problem

Meta Platforms uses feature vectors to represent content such as Instagram Reels. Implement a k-nearest neighbors classifier that predicts each validation item's label from labeled training vectors, then computes validation accuracy.

Formal Specification

Given train_features, a list of numeric vectors, and train_labels, the corresponding string labels, classify every vector in validation_features. Use squared Euclidean distance, which avoids an unnecessary square root. For each validation vector, select the k closest training vectors. The predicted label is the label with the highest frequency among those neighbors. If multiple labels tie, return the lexicographically smallest label.

Return a dictionary with predictions, a list of predicted labels in validation order, and accuracy, the fraction of predictions equal to validation_labels.

Constraints

  • 1 <= len(train_features) <= 2 * 10^3
  • 1 <= len(validation_features) <= 500
  • All vectors have the same dimension
  • 1 <= dimension <= 50
  • 1 <= k <= len(train_features)
  • Labels are non-empty strings

Function Signature

def knn_classify(train_features, train_labels, validation_features, validation_labels, k):
Your solutionPython 3
You need to log in / sign up to run or submit.
Run your code to see test output