← Openai Interview Insights

Openai·Machine Learning Engineer·Technical Phone Screen·Senior

Senior
May 2026

Summary

Interviewed for an ML Engineer role at OpenAI and got hit with a pretty theoretical question about nearest neighbor search. Short round, one meaty question, left me second-guessing my answer the whole way home.

Questions Asked (1)

Q1

If you switched from L2 to L1 distance, how would you implement 1-nearest-neighbor classification using a neural network?

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

I fumbled around for a bit trying to remember how L1 differs from L2 in terms of gradients and what that means for a network you'd actually train.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, clarify that 1-NN with L1 distance can be implemented by computing pairwise L1 distances between query and support points, then using a softmin or hard argmin to select the nearest neighbor. Then, describe how to construct a neural network that computes L1 distances and performs the selection, possibly using a differentiable relaxation for end-to-end training.

Pro tip: Mention that L1 distance is more robust to outliers than L2, and that using a differentiable softmin (e.g., with a temperature parameter) allows gradient-based learning while approximating hard 1-NN. Also, note that for large datasets, approximate nearest neighbor methods like LSH for L1 may be needed.

1. Clarify the problem and assumptions

State that 1-NN classification assigns the label of the nearest training example under L1 distance. Assume we have a support set of labeled points and a query point, and we want to implement this using a neural network, possibly for end-to-end learning.

2. Compute pairwise L1 distances

For a query x and each support point x_i, compute the L1 distance ||x - x_i||_1. This can be done by broadcasting and summing absolute differences, or by using a network layer that computes absolute differences and sums them.

3. Select the nearest neighbor

Use an argmin operation to find the index of the minimum distance. For a differentiable implementation, replace argmin with a softmin (e.g., softmax over negative distances) to get a weighted combination of labels.

4. Produce the classification output

If using hard 1-NN, output the label of the nearest neighbor. If using softmin, output a weighted vote of labels, which can be trained end-to-end with backpropagation.

5. Discuss trade-offs and scalability

Mention that L1 distance may be preferred for robustness, but computing all pairwise distances is O(ND). For large N, consider approximate methods or metric trees. Also, note that softmin temperature controls the approximation to hard 1-NN.

Key Points to Mention

  • L1 distance formula: sum of absolute differences.
  • Neural network implementation: absolute difference layer + sum reduction.
  • Differentiable relaxation: softmin/softmax with temperature for end-to-end training.
  • Hard 1-NN: non-differentiable argmin, but can be used at inference.
  • Comparison to L2: L1 is more robust to outliers, but may have different optimization properties.
  • Scalability: O(ND) computation, approximate nearest neighbor methods for large datasets.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.