Return the K Nearest Input Points to the Origin

Quick Overview

Return the K Nearest Input Points to the Origin. Return the k points from points with smallest Euclidean distance to (0,0).

Return the K Nearest Input Points to the Origin

Company: Amazon

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

### Problem Return the k points from `points` with smallest Euclidean distance to `(0,0)`. The selected collection is guaranteed unique; its returned order may be arbitrary. Compare distances using `sqrt(x*x+y*y)`, or equivalently their squared values. ```cpp class Solution { public: vector<vector<int>> kClosest(vector<vector<int>>& points, int k); }; ``` ### Examples **Example 1:** ```text Input: points = [[0,2],[3,0],[1,1]], k = 2 Output: [[1,1],[0,2]] Explanation: Squared distances are4,9,and2, so the points with distances squared2 and4 are selected. Their order may be reversed. ``` **Example 2:** ```text Input: points = [[4,0],[0,1]], k = 1 Output: [[0,1]] Explanation: Squared distance1 is smaller than16. ``` ### Constraints - `1 <= k <= points.length <= 10^4`. - `-10^4 <= x_i,y_i <= 10^4`. - The k-point answer is guaranteed unique except for its order. ### Contract considerations The answer is unique as a collection, not as an ordered array. The original any-order allowance remains intact. ```hint Read the boundary condition The square root is monotone, so squared distances give the same ranking. ``` ### What a Strong Answer Covers The answer is unique as a collection, not as an ordered array. The original any-order allowance remains intact. ### Follow-up Questions Optional practice extension, separate from the original task: How would you answer successive queries with different values of k on the same points?

Overview: Return the K Nearest Input Points to the Origin. Return the k points from points with smallest Euclidean distance to (0,0).

Amazon logo
Amazon
Source window: 0–3 months · as of Oct 21, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Problem

Return the k points from points with smallest Euclidean distance to (0,0). The selected collection is guaranteed unique; its returned order may be arbitrary. Compare distances using sqrt(x*x+y*y), or equivalently their squared values.

class Solution {
public:
    vector<vector<int>> kClosest(vector<vector<int>>& points, int k);
};

Examples

Example 1:

Input: points = [[0,2],[3,0],[1,1]], k = 2
Output: [[1,1],[0,2]]
Explanation: Squared distances are4,9,and2, so the points with distances squared2 and4 are selected. Their order may be reversed.

Example 2:

Input: points = [[4,0],[0,1]], k = 1
Output: [[0,1]]
Explanation: Squared distance1 is smaller than16.

Constraints

  • 1 <= k <= points.length <= 10^4 .
  • -10^4 <= x_i,y_i <= 10^4 .
  • The k-point answer is guaranteed unique except for its order.

Contract considerations

The answer is unique as a collection, not as an ordered array. The original any-order allowance remains intact.

What a Strong Answer Covers Guidance

The answer is unique as a collection, not as an ordered array. The original any-order allowance remains intact.

Follow-up Questions Guidance

Optional practice extension, separate from the original task: How would you answer successive queries with different values of k on the same points?