FastPrepK Closest Points to a Target
Problems/K Closest Points to a Target

K Closest Points to a Target

Asana logoAsana● MediumFULLTIMEONSITE INTERVIEW

Updated Nov 2026

QuestionFunctionExamplesConstraints

Problem statement

Source note: Reported in an Asana full-time onsite interview. The report asks for the k closest integer points to a target, using a distance function and a max heap. It does not say which distance to use, how to break ties, or in what order to output. We chose those. Our version is about 84% the same as the reported question.

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 Asana Onsite Interview questions

  • Cats, Rabbits, and Snails Board GameONSITE INTERVIEW · Seen Aug 2026
  • Layered ASCII Canvas Command ProcessorONSITE INTERVIEW · Seen Aug 2026

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 the OriginSambaNova Systems · PHONE SCREENSambaNova Systems interview questions
  • 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
  • Retrieve Vectors by Cosine SimilarityHarvey · ONSITE INTERVIEWHarvey interview questions
  • Meeting Rooms IISalesforce · ONSITE INTERVIEWSalesforce interview questions
  • Top K Largest Values from a StreamThe D.E. Shaw Group · ONSITE INTERVIEWThe D.E. Shaw Group interview questions
  • Generate Complete Inputs for a Supplied ClassifierGeneral Motors · ONSITE INTERVIEW
See Asana hiring insights
Saved
class Solution {
    public int[][] kClosestPoints(int[][] points, int[] target, int k) {
        // write your code here
    }
}
Checking account…