FastPrepK Closest Points to the Origin
Problems/K Closest Points to the Origin

K Closest Points to the Origin

SambaNova Systems logoSambaNova Systems● MediumNEW GRADPHONE SCREEN

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: A firsthand SambaNova phone interview report clearly describes the well-known K Closest Points to Origin problem. Here the target point is given as part of the input, so a fixed answer is easy to check. The examples use the origin.

Given distinct two-dimensional integer points, a two-dimensional integer target, and an integer k, return the k points closest to target.

For a point [x, y], use squared Euclidean distance (x - target[0])^2 + (y - target[1])^2. Smaller distance is closer. Break equal-distance ties by smaller x, then smaller y.

Return the selected points ordered from closest to farthest using that same distance-and-coordinate order.

Function

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

Examples

Example 1

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

The two selected points both have squared distance 8. The tie is resolved by the smaller x-coordinate.

Example 2

points = [[5,5],[3,4],[4,3],[2,2]]target = [3,3]k = 3return = [[3,4],[4,3],[2,2]]

The first two returned points have squared distance 1. [2,2] follows with squared distance 2.

Example 3

points = [[0,0]]target = [7,-4]k = 1return = [[0,0]]

The only point must be returned.

Constraints

  • 1 <= points.length <= 10^5.
  • points[i].length == 2 and target.length == 2.
  • All points are distinct.
  • 1 <= k <= points.length.
  • -10^9 <= points[i][j], target[j] <= 10^9.
  • Use 64-bit arithmetic for squared distances.

More SambaNova Systems Phone Screen questions

  • Deterministic Zigzag SortPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Minimum Window SubstringPHONE SCREEN · Seen Jun 2022
  • Number of ProvincesPHONE SCREEN · Seen Jun 2022
  • Merge Three Sorted ArraysPHONE SCREEN · Seen Jun 2022
  • Task Scheduler with CooldownPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Point Inside a TrianglePHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
  • Parse Variable, Expression, and Application TreesPHONE SCREEN · ONSITE INTERVIEW · Seen Mar 2022
  • Decode StringPHONE SCREEN · Seen Mar 2022
  • Insert, Delete, and Get Random in Constant TimePHONE SCREEN · Seen Mar 2022
  • Ugly NumberPHONE SCREEN · ONSITE INTERVIEW · Seen Apr 2021

Similar questions at other companies

  • Top K Frequent Closest PointsGoogle · PHONE SCREENGoogle interview questions
  • The k Points Nearest the OriginAdobe · PHONE SCREENAdobe interview questions
  • K Closest Points to a TargetAsana · ONSITE INTERVIEW
  • Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
  • Mutual-Friend RecommendationsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
  • Tennis Court SchedulingAtlassian · PHONE SCREEN · ONSITE INTERVIEWAtlassian interview questions
  • Maximum Number of Events That Can Be AttendedByteDance · PHONE SCREENByteDance interview questions
  • Meeting Rooms IIeBay · PHONE SCREENeBay interview questions
  • Select the Top User MatchesDuolingo · PHONE SCREENDuolingo interview questions
  • Meeting Rooms IIQuince · PHONE SCREENQuince interview questions
All SambaNova Systems interview questionsSee SambaNova Systems hiring insights
Saved
class Solution {
    public int[][] kClosestPoints(int[][] points, int[] target, int k) {
        // write your code here
    }
}
Checking account…