K Closest Points to the Origin
Updated
Problem statement
You are given an array points, where points[i] = [x, y] is a point on a plane, and an integer k. Return the k points that are nearest to the origin (0, 0), measured by straight-line (Euclidean) distance.
Return the points in this order: nearest first; when two points are equally far, the one with the smaller x first; when x is also equal, the one with the smaller y first. This makes the answer unique.
The same point can appear more than once in points. Each copy counts as its own point.
Function
kClosest(points: int[][], k: int) → int[][]Examples
Example 1
points = [[3,4],[1,-1],[0,2],[-2,0]]k = 2return = [[1,-1],[-2,0]]The squared distances are 25, 2, 4 and 4. [1,-1] is nearest. [0,2] and [-2,0] tie for second, and [-2,0] wins because its x is smaller.
Example 2
points = [[2,2],[2,-2],[-2,2]]k = 3return = [[-2,2],[2,-2],[2,2]]All three points are at the same distance, so they are ordered by x, then by y.
Example 3
points = [[5,0],[0,0],[5,0]]k = 2return = [[0,0],[5,0]]The origin itself is nearest. The two copies of [5,0] tie, and either copy gives the same output.
Example 4
points = [[7,-1]]k = 1return = [[7,-1]]With one point and k = 1, that point is the answer.
Constraints
1 <= k <= points.length <= 104points[i].length == 2-104 <= x, y <= 104
More WhatNot Phone Screen questions
- Adjacent Pair and Run RemovalPHONE SCREEN · Seen Nov 2026
- Merge Sorted Squares of Two ArraysPHONE SCREEN · Seen Nov 2026
- User Journey PathsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Knight Dialer SequencesPHONE SCREEN · Seen Oct 2026
- Reachable Exits in an Obstacle GridPHONE SCREEN · Seen Oct 2026
- Palindrome with One DeletionPHONE SCREEN · Seen Oct 2026
- Search a 2D MatrixPHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Attraction Route Without Reusing TrailsPHONE SCREEN · Seen Sep 2026
- Earliest Car for Each PassengerPHONE SCREEN · Seen Sep 2026
- Minimum Stores for a Shopping ListPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Maximum Visible DronesAnduril · PHONE SCREENAnduril interview questions
- Optimal Central WarehouseGoogle · ONSITE INTERVIEWGoogle interview questions
- Adaptive Calibration ErrorOpenAI · PHONE SCREENOpenAI interview questions
- Find All Pythagorean TripletsDell Technologies · PHONE SCREENDell Technologies interview questions
- Sort a Quadratic-Transformed ArrayLinkedIn · PHONE SCREENLinkedIn interview questions
- Painting Arcs to Nearest Neighbors on a CircleSusquehanna International Group (SIG) · PHONE SCREENSusquehanna International Group (SIG) interview questions
- Bootstrap Mean and Confidence IntervalAmazon · PHONE SCREEN
- Find Missing and Duplicate IntegerInMobi · PHONE SCREENInMobi interview questions
- The k Points Nearest the OriginAdobe · PHONE SCREENAdobe interview questions
- Count Centaur Friend RequestsMeta · PHONE SCREENMeta interview questions