K Closest Feature Vectors
You have a list of points (feature vectors of the same dimension) and a query vector. Return the k points nearest to the query by Euclidean distance, nearest first. This is the lookup step of a k-nearest-neighbours classifier.
If two points are equally far, the one that appears earlier in points comes first. If k is larger than the number of points, return all of them.
Example 1
Input: points = [[1,3],[-2,2],[5,8],[0,1]], query = [0,0], k = 2 Output: [[0,1],[-2,2]]
Example 2
Input: points = [[1,0],[0,1],[-1,0],[0,-1]], query = [0,0], k = 2 Output: [[1,0],[0,1]] Explanation: All four are at distance 1; ties keep input order.
Constraints
- 0 <= points.length <= 10^4
- 1 <= dimension <= 64
- 0 <= k
Hint 1
Compare squared distances; the square root does not change the order.
Hint 2
Break ties on the original index so the result is deterministic.
Good practice for: Machine Learning Engineer, Data Scientist, AI Engineer (LLM & Generative AI), Backend Developer, MLOps Engineer, Game Developer
Run checks your code on 3 sample tests. Submit runs all 8 tests, including hidden ones, and earns points. Use console.log to debug. Python's first run can take a few seconds while it starts.