One-Nearest-Neighbor Classification
Reported by candidates from OpenAI's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.
The OpenAI OA reported in August 2026 looks like a machine learning question, but it's a brute-force loop with one trap. You get training vectors, labels, and queries, and you return the label of the nearest training sample for each query. Most people lose points on the tie rule, not the distance math. If you blank on the details mid-assessment, StealthCoder sits invisibly on your screen and gives you a working solution in real time. Either way, this one is very doable if you read the tie-breaking line twice before you type anything.
The problem
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 squared Euclidean distance: distance(a, b) = sum((a[i] - b[i])^2) If several training samples have the same minimum distance, choose the one that appears earliest in trainingFeatures. Return the chosen training label for every query, in query order. 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 tied at squared distance 2, so the earlier sample supplies label 10. For [4,4], the nearest sample is [5,5], which supplies label 30. Example 2 trainingFeatures = [[0],[2]] trainingLabels = [7,9] queries = [[1]] return = [7] The query is one unit from both training samples, so the stable tie rule selects the first label, 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.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The pattern is plain array iteration. For each query, scan every training sample, compute the squared Euclidean distance, and track the best distance and its index. Skip the square root. Squared distance preserves ordering and keeps everything in integers. The mistake that sinks first attempts is the comparison operator. Use strictly less than when updating the best, so the earliest sample wins ties. Using less-than-or-equal silently picks the last tied sample and fails Examples 1 and 2. Also watch overflow in languages with 32-bit ints: differences go up to 20000, squared is 4x10^8, summed over 100 dimensions that needs 64-bit. Cost is 2000 x 2000 x 100, about 4x10^8 operations, so keep the inner loop tight and avoid allocating per pair. StealthCoder is your hedge if the loop bounds or tie logic slip under pressure.
If you see this problem in your OA tomorrow, the play is to recognize the pattern in 30 seconds. StealthCoder buys you that recognition.
You can drill One-Nearest-Neighbor Classification cold, or you can hedge it. StealthCoder runs invisibly during screen share and surfaces a working solution in under 2 seconds. The proctor sees the IDE. They don't see what's behind it. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Built by an Amazon engineer who passed his OA cold and still thinks the filter is broken. Works on HackerRank, CodeSignal, CoderPad, and Karat.
One-Nearest-Neighbor Classification FAQ
How hard is the OpenAI one-nearest-neighbor question really?+
Easy on algorithm, moderate on care. There's no clever data structure needed. It's a double loop with a distance function. Points get lost on the tie rule and integer overflow, not on the idea. If you can write nested loops cleanly, you can finish this.
What's the trick to the tie-breaking rule?+
Update your best candidate only when the new distance is strictly smaller. Since you scan training samples in order, the first one to reach the minimum stays. Using less-than-or-equal flips the answer to the last tied sample and breaks the examples.
Do I need a KD-tree or anything fancy?+
No. With up to 2000 training samples, 2000 queries, and 100 dimensions, brute force is about 4x10^8 simple operations. KD-trees degrade in high dimensions anyway. Write the straightforward loop and keep the inner work minimal, with no allocations per pair.
Should I take the square root of the distance?+
No. Squared distance orders samples the same way and stays an integer, so you avoid floating-point error and ties that look unequal by rounding. The problem even defines distance as the squared sum, so compare those values directly.
How do I prepare in 48 hours for this OpenAI OA?+
Practice writing a clean nested-loop scan with a running best value and index. Test it against the three examples, especially the tie case and the exact-match case. Also check your integer type handles large sums. Then rehearse a few similar array scans so the syntax is automatic.