Skip to interview prep
Apexed

60. K Closest Points to the Origin

MediumPriority queues & heaps

Return the k points closest to (0, 0) by Euclidean distance. Order them by distance, breaking ties by x then y.

Example 1

Input: points = [[1, 3], [-2, 2]], k = 1
Output: [[-2, 2]]

Constraints

  • 1 ≤ k ≤ points.length ≤ 10⁴

Tab indents · Ctrl/⌘ + Enter runs · Esc then Tab leaves the editor

Run checks these cases; the expected output comes from the reference solution. Submit judges all 3 hidden and sample tests.

Code runs only in your browser in an isolated sandbox. Progress and submissions are saved on this device.

K Closest Points to the Origin — coding interview practice — Apexed