FastPrepThe k Points Nearest the Origin
Problems/The k Points Nearest the Origin

The k Points Nearest the Origin

Adobe logoAdobe● MediumFULLTIMEPHONE SCREEN
QuestionFunctionExamplesConstraints

Problem statement

Source note: In a C++ first round the coding question was the classic k closest points to the origin; the rest of the round was about smart pointers and class basics.

The report gives no limits, examples or output order. Here the answer is sorted by distance with ties broken by x and then y, duplicates count separately, there are at most 3000 points with coordinates from -10^4 to 10^4. The examples are our own.

The judged task matches the report at about 90%.

You are given a list of points on a plane, points[i] = [x, y], and an integer k. Return the k points closest to the origin [0, 0], using the usual straight-line distance sqrt(x^2 + y^2).

Return them in order: closest first. If two points are equally far, the one with the smaller x comes first, and if x is also equal, the smaller y. A point that appears twice in the list counts as two points.

Function

kClosestPoints(points: int[][], k: int) → int[][]

Examples

Example 1

points = [[2,2],[-1,1],[0,3],[1,-1]]k = 2return = [[-1,1],[1,-1]]

The squared distances are 8, 2, 9 and 2. The two closest points, [-1, 1] and [1, -1], tie at 2, so the smaller x comes first.

Example 2

points = [[5,0],[0,5],[0,-5],[-5,0]]k = 3return = [[-5,0],[0,-5],[0,5]]

All four points are at distance 5. Ordered by x and then y they are [-5, 0], [0, -5], [0, 5], [5, 0]; the first three are returned.

Example 3

points = [[7,7]]k = 1return = [[7,7]]

Only one point, so it is returned.

Constraints

  • 1 <= k <= points.length <= 3000
  • points[i].length == 2
  • -10^4 <= x, y <= 10^4

More Adobe problems

  • Evaluate a Parenthesized Integer ExpressionPHONE SCREEN · Seen Oct 2026
  • All Elements of Two BSTs in OrderPHONE SCREEN · Seen Aug 2026
  • Rotate an Array In PlaceOA · PHONE SCREEN · Seen Aug 2026
All Adobe interview questionsSee Adobe hiring insights
Saved
public int[][] kClosestPoints(int[][] points, int k) {
    // Write your code here.
}
Checking account…