FastPrepOne-Nearest-Neighbor Classification
Problems/One-Nearest-Neighbor Classification

One-Nearest-Neighbor Classification

OpenAI logoOpenAI● EasyNEW GRADFULLTIMEPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an OpenAI phone screen for new grad and full-time roles. The original report names the ordinary one-nearest-neighbor classification exercise. It does not keep how the data is shown, the distance measure, the tie rule, the function interface, or the limits. Here we use integer feature vectors and squared Euclidean distance. Ties go to the earlier item in the input.

You are given labeled training samples and some query samples. Each sample is a list of integers, called a feature vector. trainingFeatures holds the training vectors, and there is at least one. trainingLabels holds one label for each training vector. queries holds the query vectors.

For each query, find the one training sample with the smallest squared Euclidean distance to it:

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

If several training samples tie for the smallest distance, pick the one that comes first in trainingFeatures.

Return the label of the chosen training sample for each query, in the same order as the queries.

Function

classifyOneNearestNeighbor(trainingFeatures: int[][], trainingLabels: int[], queries: int[][]) → int[]

Examples

Example 1

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

For [1,1], the first two training samples are both at squared distance 2. That is a tie, so the earlier sample wins and the label is 10. For [4,4], the closest training sample is [5,5], so the label is 30.

Example 2

trainingFeatures = [[0],[2]]trainingLabels = [7,9]queries = [[1]]return = [7]

The query is one unit away from both training samples. That is a tie, so the first sample wins and the answer is 7.

Example 3

trainingFeatures = [[-2,1],[3,4]]trainingLabels = [5,8]queries = [[3,4],[-1,1]]return = [8,5]

The first query exactly matches the second training sample. The second query is closest to [-2,1].

Constraints

  • 1 <= trainingFeatures.length == trainingLabels.length <= 2000.
  • 1 <= queries.length <= 2000.
  • 1 <= trainingFeatures[i].length == queries[j].length <= 100.
  • -10^4 <= trainingFeatures[i][k], queries[j][k] <= 10^4.
  • Every squared-distance sum fits in a signed 64-bit integer.

More OpenAI Phone Screen questions

  • Secret Number Search with One-Call-Delayed FeedbackPHONE SCREEN · Seen Nov 2026
  • Determine the Winner of Three-Card HandsPHONE SCREEN · Seen Nov 2026
  • Grid Infection with Recovery After D DaysPHONE SCREEN · Seen Nov 2026
  • Sparse Plant Infection SimulationPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Light Source Hits: Sample, Compare, and FitPHONE SCREEN · Seen Nov 2026
  • Rectangle Point Coverage CountsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
  • Adaptive Calibration ErrorPHONE SCREEN · Seen Nov 2026
  • Elevator Scheduling SimulationPHONE SCREEN · Seen Nov 2026
  • Evaluate Noisy AnnotatorsPHONE SCREEN · Seen Oct 2026
  • Network Endpoint or Cycle BoundaryOA · PHONE SCREEN · Seen Oct 2026

Similar questions at other companies

  • Multiply Two MatricesAdobe · PHONE SCREENAdobe interview questions
  • Matrix Multiplication With and Without NumPyNVIDIA · PHONE SCREENNVIDIA interview questions
  • Adjacent Order-Statistic Gap DistributionsScale AI · PHONE SCREENScale AI interview questions
  • Scaled Dot-Product AttentionAmazon · PHONE SCREEN
  • Rotate ImageTokopedia · PHONE SCREENTokopedia interview questions
  • Rotate ImageSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
  • Survey Table: Group Given an AnswerSusquehanna International Group (SIG) · OASusquehanna International Group (SIG) interview questions
  • Rotate a Flat Image ClockwiseGoogle · ONSITE INTERVIEWGoogle interview questions
  • Matrix MultiplicationeBay · ONSITE INTERVIEWeBay interview questions
  • Linear Layer Forward and Backward PassesMeta · ONSITE INTERVIEWMeta interview questions

OpenAI interview experiences

  • OpenAI OA: What It Tests (2026)
All OpenAI interview questionsSee OpenAI hiring insights
Saved
class Solution {
    public int[] classifyOneNearestNeighbor(int[][] trainingFeatures, int[] trainingLabels, int[][] queries) {
        // Write your code here.
    }
}
Checking account…