K Closest Points to Origin
Solve this the way you would in the interview: out loud, on a timer, with an AI interviewer asking follow-ups while you code. You get a scored report at the end.
Practice this as a mock interviewFree to start, no account neededProblem
You are given a list of points on a 2D plane, where each entry points[i] = [x, y] represents the coordinates of a single point. You are also given an integer k.
Return the k points that are **closest** to the origin (0, 0).
Distance between two points is measured using the standard **Euclidean distance**: the distance from (x, y) to the origin is sqrt(x*x + y*y). Note that to compare distances you do not actually need the square root, since comparing the squared distances x*x + y*y preserves the same ordering.
The answer may be returned in **any order**, and it is guaranteed to be **unique** except for the order of the returned points (no two points are equidistant in a way that would cause ambiguity about which k to pick).
Each returned point should be a two-element list [x, y] taken directly from the input.
Examples
Constraints
- `1 <= k <= points.length <= 10^4`
- `-10^4 <= xi, yi <= 10^4`
- Each `points[i]` has exactly two integer coordinates `[xi, yi]`.
- The set of `k` closest points is unique; only their order may vary.
What the interviewer grades
A correct solution is one of five scores. The report also grades problem understanding (did you clarify inputs and edge cases before coding), problem solving, communication while you work, code quality, and testing. See a sample report, or read up on the pattern in the lessons.