One-Nearest-Neighbor Classification with Manhattan Distance
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 September 2026 looks like a gimme: one-nearest-neighbor with Manhattan distance. Then the tie-break rule shows up and a sloppy loop quietly returns the wrong label. If your OA invite is sitting in your inbox, this is the kind of problem where the logic is easy and the details are what fail you. It's a brute-force scan over training rows for every query, with a strict comparison to keep the earliest minimum. StealthCoder is there as a safety net on the live OA if you blank, but you should be able to write this cold after reading the next paragraph.
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 Manhattan distance: distance(a, b) = sum(abs(a[i] - b[i])) 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 classifyOneNearestNeighborManhattan(trainingFeatures: int[][], trainingLabels: int[], queries: int[][]) → int[] Examples Example 1 trainingFeatures = [[0,0],[3,0],[2,3]] trainingLabels = [10,20,30] queries = [[2,1],[2,3]] return = [20,30] The first query is two units from both the first and second rows, so the earlier of those tied nearest rows supplies label 20 only after checking all distances: its distances are 3, 2, and 2, making the second row the earliest minimum. The second query exactly matches the third row. Example 2 trainingFeatures = [[-2,1],[2,1]] trainingLabels = [7,9] queries = [[0,1],[3,2]] return = [7,9] The first query ties at Manhattan distance two and selects the earlier row; the second 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 Manhattan-distance sum fits in a signed 64-bit integer.
Reported by candidates. Source: FastPrep
Pattern and pitfall
The trick is that there is no trick. Constraints cap training and queries at 2000 each with up to 100 dimensions, so the nested loop is about 400 million simple operations at worst. Brute force is the intended answer. For each query, scan training rows in order, compute the sum of absolute differences, and update the best only when the new distance is strictly less than the current best. Strict less-than is the whole tie-break. Use <= and you pick the latest tie instead of the earliest. Initialize best distance to a huge value or to the first row's distance, not zero. You can also break early inside the distance sum once it exceeds the current best, which saves real time. Use 64-bit integers if your language needs it. If you freeze on the live OA, StealthCoder can hand you the loop structure, but the logic is short enough to own yourself.
Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.
You can drill One-Nearest-Neighbor Classification with Manhattan Distance 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. Made for the candidate who got the OA invite this morning and has 72 hours, not six months.
Get StealthCoderRelated leaked OAs
You've seen the question.
Make sure you actually pass OpenAI's OA.
OpenAI reuses patterns across OAs. Made for the candidate who got the OA invite this morning and has 72 hours, not six months. Works on HackerRank, CodeSignal, CoderPad, and Karat.
One-Nearest-Neighbor Classification with Manhattan Distance FAQ
What's the trick in the OpenAI one-nearest-neighbor problem?+
There's no clever algorithm. It's a brute-force scan per query. The only thing that matters is the tie-break: update your best match only when the new distance is strictly smaller, so the earliest training row wins ties.
Will brute force pass the constraints?+
Yes. With up to 2000 training rows, 2000 queries, and 100 dimensions, you're near 400 million basic operations. That's the intended scale. Add an early break when the running distance already exceeds your best to cut the work further.
Where do people get this wrong?+
They use <= instead of < when comparing distances, which picks the last tied row instead of the first. Example 1 in the problem tests exactly this. The other slip is initializing the best distance to zero instead of infinity.
Do I need a KD-tree or any spatial index?+
No. Manhattan distance in up to 100 dimensions makes KD-trees nearly useless anyway, and the constraints are sized for brute force. Writing a fancy structure only adds bug surface and the tie-break gets harder to enforce.
How do I prepare for this in 48 hours?+
Write the nested loop from memory twice, then test it on both examples, especially the tie case. Check negative coordinates and a single-dimension input. Keep the return as a list of labels in query order, not indices.