Pick the K Nearest Grid Points to (0, 0)
Updated
Problem statement
Each entry points[i] = [x, y] names a spot on a grid. Choose the k spots nearest to (0, 0), measured as straight-line distance, and list them with the nearest first.
Spots equally far from (0, 0) are ranked by their position in points, earlier first; this decides which of several tied spots make the cut as well as their order. Return the spots themselves rather than distances.
Function
kClosest(points: int[][], k: int) → int[][]Examples
Example 1
points = [[6,8],[-2,1],[3,3],[0,-5]]k = 2return = [[-2,1],[3,3]]Squared distances from the origin: 100, 5, 18 and 25. The two smallest are [-2,1] (5) and [3,3] (18), listed nearest first.
Example 2
points = [[1,7],[4,4],[-7,1],[5,5],[2,2]]k = 3return = [[2,2],[4,4],[1,7]]Squared distances: 50, 32, 50, 50, 8. [2,2] and [4,4] are nearest; then three points tie at 50 and the earliest in the input, [1,7], takes the last place.
Example 3
points = [[-1,2],[2,1],[0,0],[-2,-1]]k = 4return = [[0,0],[-1,2],[2,1],[-2,-1]]All points are wanted. [0,0] is nearest, and the other three tie at squared distance 5, so they follow in input order.
Constraints
1 <= k <= points.length <= 1000-10^4 <= x, y <= 10^4
More Capital One interview questions
- Count Records Present in A but Not BPHONE SCREEN · Seen Oct 2026
- LRU CachePHONE SCREEN · ONSITE INTERVIEW · Seen May 2026
- Maximum Subarray SumPHONE SCREEN · ONSITE INTERVIEW · Seen Aug 2025
- Ten-Pin Bowling Frame ScoresPHONE SCREEN · Seen Nov 2023
- Min StackPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2023
- Invert a Binary TreePHONE SCREEN · Seen Dec 2020
- Count How Often a Word Appears in a ParagraphPHONE SCREEN
- In-Memory File System With Directory SizesPHONE SCREEN · ONSITE INTERVIEW
- The Nth Number in the Fibonacci SequencePHONE SCREEN · ONSITE INTERVIEW
- Digit-Coded Card ValidationONSITE INTERVIEW · Seen Nov 2026
Similar questions at other companies
- Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEW
- Adaptive Calibration ErrorOpenAI · PHONE SCREEN
- Find All Pythagorean TripletsDell Technologies · PHONE SCREEN
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREEN
- Optimal Central WarehouseGoogle · ONSITE INTERVIEW
- Bootstrap Mean and Confidence IntervalAmazon · PHONE SCREEN
- Find Missing and Duplicate IntegerInMobi · PHONE SCREEN
- Build a Precision-Recall CurveMicrosoft · ONSITE INTERVIEW
- Count Centaur Friend RequestsMeta · PHONE SCREEN
- Minimum Fuel Car AlignmentFivetran · PHONE SCREEN