FastPrepOne-Nearest-Neighbor Classification with Manhattan Distance

One-Nearest-Neighbor Classification with Manhattan Distance

OpenAI logoOpenAI● EasyFULLTIMEPHONE SCREEN

Problem statement

You are given a nonempty set of labeled training samples and a set of query samples. Each sample is an integer feature vector.

For every query, find the single training sample with the smallest Manhattan distance:

distance(a, b) = sum(abs(a[i] - b[i]))

The problem statement continues
Pro

Examples

Example 1

trainingFeatures = [[0,0],[3,0],[2,3]]trainingLabels = [10,20,30]queries = [[2,1],[2,3]]return = [20,30]

The first query is two units from both the first and second rows, so the earlier of those tied nearest rows supplies label 20 only after checking all distances: its distances are 3, 2, and 2, making the second row the earliest minimum. The second query exactly matches the third row.

FastPrep Pro
Reported in 1 OpenAI interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week
See OpenAI hiring insights
CodePython 3
Run and Submit unlock with Pro
FastPrep Pro
Reported in 1 OpenAI interview this week

Unlock this recently reported problem

FastPrep Pro gives you full access to interview problems reported within the last week.

  • Full problem statement and constraints
  • 1 more worked example, explained
  • Guided hints and editorial
  • Run your code on real test cases
$8.25/month

$99 billed yearly — or $19 month-to-month. Cancel anytime.

Free plan — 2 of 2 free unlocks used this week