Deterministic K-Means Assignments
Updated
Problem statement
Implement K-Means clustering for integer points, with no randomness. Use the first k points as the initial centroids, in that order.
Repeat these steps until no point changes its assigned cluster:
- Assign every point to the centroid with the smallest squared Euclidean distance. Break a tie by choosing the smaller cluster index.
- Replace each nonempty cluster's centroid with the coordinate-wise arithmetic mean of its assigned points. If a cluster is empty, keep its previous centroid.
Return an integer array whose value at index i is the zero-based cluster index assigned to points[i]. Implement the arithmetic and iteration directly; do not call a clustering library.
Function
clusterPoints(points: int[][], k: int) → int[]Examples
Example 1
points = [[0,0],[10,10],[1,0],[9,10]]k = 2return = [0,1,0,1]The first two points initialize the two centroids. The two points near the origin join cluster 0, and the two points near [10,10] join cluster 1. Recomputed centroids preserve those assignments.
Example 2
points = [[0],[4],[2]]k = 2return = [0,1,0]The point [2] is initially equally distant from centroids 0 and 4, so the smaller cluster index wins the tie.
Example 3
points = [[-5,2],[7,-1],[3,4]]k = 3return = [0,1,2]When every point initializes its own cluster, each point remains at distance zero from that centroid.
Constraints
1 <= points.length <= 2001 <= points[i].length <= 10- All points have the same dimension.
1 <= k <= min(points.length, 10)- The first
kpoints are pairwise distinct. -10000 <= points[i][j] <= 10000
More Microsoft Phone Screen questions
- Config Store with Priority OverridesPHONE SCREEN · Seen Nov 2026
- Corporation Reporting LinesPHONE SCREEN · Seen Oct 2026
- Random Forest Classifier from ScratchPHONE SCREEN · Seen Oct 2026
- Least Recently Used CacheOA · PHONE SCREEN · ONSITE INTERVIEW · Seen Oct 2026
- Greedy and Beam Sentence DecodingPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Top K Frequent ElementsPHONE SCREEN · Seen Sep 2026
- 3SumPHONE SCREEN · ONSITE INTERVIEW · Seen Sep 2026
- Arithmetic Expression EvaluatorPHONE SCREEN · Seen Sep 2026
- Employee Hierarchy Cycle and Depth AnalysisPHONE SCREEN · Seen Sep 2026
- Most Visited String SectorsPHONE SCREEN · Seen Sep 2026
Similar questions at other companies
- Deterministic K-Means AssignmentsLyft · PHONE SCREENLyft interview questions
- Online Chess Move ValidationOpenAI · PHONE SCREENOpenAI interview questions
- Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Knockout Tournament Win ProbabilityGoogle · PHONE SCREENGoogle interview questions
- Scaled Dot-Product AttentionAmazon · PHONE SCREEN
- Implement K-Means ClusteringApple · PHONE SCREENApple interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
- Fixed-Round K-Means AssignmentsNVIDIA · ONSITE INTERVIEWNVIDIA interview questions
- Automation Pipeline, Part 1: Single WorkerPlaid · PHONE SCREENPlaid interview questions
- Constant-Time Weighted Pick with an Alias TableEtched · PHONE SCREEN