K Closest Points to Origin
Given an array of `points` where `points[i] = [xi, yi]` represents a point on the X-Y plane and an integer `k`, return the `k` closest points to the origin `(0, 0)`. The distance between two points on the X-Y plane is the Euclidean distance (`√(x1 - x2)^2 + (y1 - y2)^2`). You may return the answer in any order. The answer is guaranteed to be unique (except for the order that it is in). **Examples:** ```typescript kClosest([[1,3],[-2,2]], 1) // [[-2,2]] kClosest([[3,3],[5,-1],[-2,4]], 2) // [[3,3],[-2,4]] ```
Solve It in the BrowserPractice Explaining It Out Loud
Optimal Approach
- Time complexity: O(n log n)
- Space complexity: O(1)
Sorting points by squared Euclidean distance takes O(n log n) time (or O(n log k) with max-heap).
Follow-Up Questions Interviewers Ask About K Closest Points to Origin
- Why avoid computing the square root when comparing distances?
Since square root is monotonically increasing, comparing x^2 + y^2 gives the exact same ordering without expensive floating point operations.
Practice the full problem with execution, tests, and AI code review in the interactive lab — then rehearse explaining your solution in a live voice mock interview.