PracHub
QuestionsPremiumLearningGuidesCheatsheetNEWCoaches
|Home/Machine Learning/OpenAI

Implement 1NN with NumPy

Last updated: May 19, 2026

Quick Overview

This question evaluates implementing a 1-nearest-neighbor classifier with NumPy, testing skills in vectorized numerical computation, distance metrics, tie-breaking label handling, and translating algorithms into linear-algebra operations.

  • medium
  • OpenAI
  • Machine Learning
  • Machine Learning Engineer

Implement 1NN with NumPy

Company: OpenAI

Role: Machine Learning Engineer

Category: Machine Learning

Difficulty: medium

Interview Round: Technical Screen

Implement a 1-nearest-neighbor classifier from scratch using NumPy. You are given: - `X_train`: a NumPy array of shape `(n_train, d)` containing training feature vectors. - `y_train`: a NumPy array of shape `(n_train,)` containing labels. - `X_test`: a NumPy array of shape `(n_test, d)` containing query feature vectors. Tasks: 1. Write a vectorized NumPy implementation that predicts the label of each test example using squared Euclidean distance. If there is a tie, return the label of the earliest training example among the tied nearest neighbors. 2. Show how the same nearest-neighbor decision can be represented as a neural-network-style computation with fixed weights and biases. You do not need to train the network; derive the weights and biases from `X_train`.

Quick Answer: This question evaluates implementing a 1-nearest-neighbor classifier with NumPy, testing skills in vectorized numerical computation, distance metrics, tie-breaking label handling, and translating algorithms into linear-algebra operations.

Related Interview Questions

  • Defend a Research Direction and Experiment Design - OpenAI (medium)
  • Implement Backprop for a Tiny Network - OpenAI (hard)
  • Filter Bad Human Annotations - OpenAI (medium)
  • Debug MiniGPT and Backpropagate Matmul - OpenAI (medium)
  • Compute Matrix Prefix Products And Gradients - OpenAI (hard)
OpenAI logo
OpenAI
May 19, 2026, 12:00 AM
Machine Learning Engineer
Technical Screen
Machine Learning
5
0

Implement a 1-nearest-neighbor classifier from scratch using NumPy.

You are given:

  • X_train : a NumPy array of shape (n_train, d) containing training feature vectors.
  • y_train : a NumPy array of shape (n_train,) containing labels.
  • X_test : a NumPy array of shape (n_test, d) containing query feature vectors.

Tasks:

  1. Write a vectorized NumPy implementation that predicts the label of each test example using squared Euclidean distance. If there is a tie, return the label of the earliest training example among the tied nearest neighbors.
  2. Show how the same nearest-neighbor decision can be represented as a neural-network-style computation with fixed weights and biases. You do not need to train the network; derive the weights and biases from X_train .

Solution

Show

Comments (0)

Sign in to leave a comment

Loading comments...

Browse More Questions

More Machine Learning•More OpenAI•More Machine Learning Engineer•OpenAI Machine Learning Engineer•OpenAI Machine Learning•Machine Learning Engineer Machine Learning
PracHub

Master your tech interviews with 7,500+ real questions from top companies.

Product

  • Questions
  • Learning Tracks
  • Interview Guides
  • Resources
  • Premium
  • For Universities
  • Student Access

Browse

  • By Company
  • By Role
  • By Category
  • Topic Hubs
  • SQL Questions
  • Compare Platforms
  • Discord Community

Support

  • support@prachub.com
  • (916) 541-4762

Legal

  • Privacy Policy
  • Terms of Service
  • About Us

© 2026 PracHub. All rights reserved.