Reported October 2019
SambaNova Systemssorting

K Closest Points to the Origin

Reported by candidates from SambaNova Systems's online assessment. Pattern, common pitfall, and the honest play if you blank under the timer.

Get StealthCoderRuns invisibly during the live SambaNova Systems OA. Under 2s to a working solution.
Founder's read

With up to 10^5 points, an O(n^2) approach is dead on arrival, and that's the whole point of this SambaNova Systems question reported in October 2019. It's K Closest Points, but aimed at an arbitrary target instead of the origin, with a strict tie-break on x then y. The pattern is sorting or a bounded heap, and the traps are overflow and ordering. If you blank during the live OA, StealthCoder runs invisibly as a safety net and gives you the structure while you type.

The problem

Given distinct two-dimensional integer points, a two-dimensional integer target, and an integer k, return the k points closest to target.
For a point [x, y], use squared Euclidean distance (x - target[0])^2 + (y - target[1])^2. Smaller distance is closer. Break equal-distance ties by smaller x, then smaller y.
Return the selected points ordered from closest to farthest using that same distance-and-coordinate order.

Function
kClosestPoints(points: int[][], target: int[], k: int) → int[][]

Examples
Example 1
points = [[1,3],[-2,2],[2,-2],[4,0]]
target = [0,0]
k = 2
return = [[-2,2],[2,-2]]
The two selected points both have squared distance 8. The tie is resolved by the smaller x-coordinate.
Example 2
points = [[5,5],[3,4],[4,3],[2,2]]
target = [3,3]
k = 3
return = [[3,4],[4,3],[2,2]]
The first two returned points have squared distance 1. [2,2] follows with squared distance 2.
Example 3
points = [[0,0]]
target = [7,-4]
k = 1
return = [[0,0]]
The only point must be returned.

Constraints
1 <= points.length <= 10^5.
points[i].length == 2 and target.length == 2.
All points are distinct.
1 <= k <= points.length.
-10^9 <= points[i][j], target[j] <= 10^9.
Use 64-bit arithmetic for squared distances.

Reported by candidates. Source: FastPrep

Pattern and pitfall

The trick is that you only need the k best points, and the ordering is a total order: squared distance, then x, then y. Since all points are distinct, no two points tie on all three keys. The simple answer is sort everything by (dist, x, y) and slice the first k. That's O(n log n) and fine for 10^5. If you want O(n log k), keep a max-heap of size k using the same comparator reversed. The pitfalls are real. Coordinates reach 10^9, so differences reach 2*10^9 and squares reach 4*10^18, and the sum of two squares can hit 8*10^18. That fits in a signed 64-bit integer (max about 9.2*10^18), but it overflows 32-bit, so use long. Don't use sqrt, because floats lose precision. Return the output sorted, not in heap order. StealthCoder is your hedge in the live OA if the comparator or overflow detail slips your mind.

Drill it cold or hedge it with StealthCoder. Either way, don't walk into the OA hoping you remember the trick.

If this hits your live OA

You can drill K Closest Points to the Origin 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 StealthCoder

Related leaked OAs

⏵ Practice the LeetCode equivalent

This OA pattern shows up on LeetCode as k closest points to origin. If you have time before the OA, drill that.

⏵ The honest play

You've seen the question. Make sure you actually pass SambaNova Systems's OA.

SambaNova Systems 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.

K Closest Points to the Origin FAQ

What's the trick in K Closest Points to the Origin with a custom target?+

Compute squared distance to the target, skip the square root, and sort with a three-key comparator: distance, then x, then y. Take the first k. The target just shifts the subtraction. Everything else matches the classic problem.

Do I need a heap or is sorting enough for 10^5 points?+

Sorting is enough. O(n log n) on 10^5 points is trivial. A size-k max-heap gives O(n log k) and looks nicer, but you must still sort the final k points. Quickselect works too, though the tie-break makes it fiddlier.

Where do people lose points on this one?+

Overflow and tie-breaking. Squared distances can reach about 8*10^18, so you need 64-bit integers. People also forget the x then y tie-break, or return heap order instead of closest-to-farthest order. Check example 1 against your comparator.

Why not use sqrt for distance?+

Squared distance preserves ordering and stays exact in integers. Square roots introduce floating-point rounding, which can break ties between equal distances. The problem statement explicitly tells you to use squared Euclidean distance with 64-bit arithmetic.

How do I prepare for this in 48 hours?+

Write it twice from memory: once with a sort and a custom comparator, once with a size-k heap. Test the three examples, especially the tie case in example 1. Then test extreme coordinates like 10^9 and -10^9 to confirm no overflow.

Problem reported by candidates from a real Online Assessment. Sourced from a publicly-available candidate-aggregated repository. Not affiliated with SambaNova Systems.

OA at SambaNova Systems?
Invisible during screen share
Get it