K Closest Points to the Origin
Updated
Problem statement
Each row of points is a two-dimensional integer point [x, y]. Return the k points closest to the origin (0, 0) by Euclidean distance. Compare squared distances x² + y²; taking square roots is unnecessary.
For this exercise, return the selected points in their original input order. The selected set is unique for every valid input, so no boundary tie needs to be broken.
Function
kClosest(points: int[][], k: int) → int[][]Examples
Example 1
points = [[1,3],[-2,2]]k = 1return = [[-2,2]]The squared distances are 10 and 8, so only the second point is selected.
Example 2
points = [[3,3],[5,-1],[-2,4]]k = 2return = [[3,3],[-2,4]]The squared distances are 18, 26, and 20. The first and third points are closest.
Example 3
points = [[1,1],[0,1],[8,0]]k = 2return = [[1,1],[0,1]]Both first points are selected; the returned rows retain input order even though the second point is nearer.
Constraints
1 <= k <= points.length <= 10000.- Each point has exactly two integer coordinates between
-10000and10000. - The set of the
kclosest points is unique.
More Meta Onsite Interview questions
- Multi-Head Attention Forward PassPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Run-Length Encoded Vector Dot ProductPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Mutual-Friend RecommendationsPHONE SCREEN · ONSITE INTERVIEW · Seen Nov 2026
- Deterministic K-Means AssignmentsPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Linear Layer Forward and Backward PassesONSITE INTERVIEW · Seen Sep 2026
- Reservoir Sampling with Recorded DrawsONSITE INTERVIEW · Seen Sep 2026
- Stochastic Gradient Descent with MomentumONSITE INTERVIEW · Seen Sep 2026
- Top K Largest Values with a HeapONSITE INTERVIEW · Seen Sep 2026
- Partition a Card Deck into FifteensONSITE INTERVIEW · Seen Sep 2026
- Insert into a Sorted Circular ListONSITE INTERVIEW · Seen Aug 2026
Similar questions at other companies
- Top K Frequent ElementsAmazon · OA · PHONE SCREEN · ONSITE INTERVIEW
- 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
- Minimum Meeting Rooms Without a Sweep LineGoldman Sachs · ONSITE INTERVIEWGoldman Sachs interview questions
- Dominant Index with Strict Doubling GapsGoogle · ONSITE INTERVIEWGoogle interview questions
- Top K Frequent ElementsCapgemini · ONSITE INTERVIEWCapgemini interview questions
- Kth Largest Element in an ArrayOneTrust · ONSITE INTERVIEW
Meta interview experiences
- Meta PhD SWE Intern AI Coding Phone Screen: 1 Experience
- Meta Business Support Engineer Phone Screen: 1 Experience
- Meta Software Engineer Onsite Questions: 3 Experiences
- Meta AI Native Engineer OA and Onsite: 1 Experience
- Meta AI-Native Coding Interview: 3 Experiences
- Meta Production Engineer Intern Questions: 6 Experiences