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.