Fixed-Round K-Means Assignments
Updated
Problem statement
Implement k-means clustering by hand for points with integer coordinates. Each row of points is one point, and all points have the same number of coordinates. Each row of initialCenters is a starting center, and its row index is its cluster index. Points may repeat, and starting centers may repeat too.
Run iterations Lloyd updates. Each update has two steps:
- Assign every point to the nearest old center. Distance is the squared Euclidean distance,
sum_a((point[a] - center[a])^2). If several centers are exactly tied for nearest, pick the one with the smallest cluster index. Keep all old centers fixed until every point has been assigned. - Move each center that got at least one point to the average of its points, coordinate by coordinate. A repeated point counts once for each time it appears. A center with no points stays where it was. Move all centers at the same time.
After the last update, assign every point one more time to the final centers, with the same tie rule. Return one cluster index per input point, in input order. Cluster indices start at zero. When iterations = 0, assign each point straight to the starting centers.
Use exact values for the averages and the distance comparisons. Do not round or truncate centers, and do not treat nearly equal distances as ties. Write the assignment and update math yourself, without a clustering library. Stop after exactly iterations updates, even if the centers are still moving.
Function
fixedRoundKMeans(points: int[][], initialCenters: int[][], iterations: int) → int[]Examples
Example 1
points = [[0,0],[10,10],[1,0],[9,10]]initialCenters = [[0,0],[10,10]]iterations = 2return = [0,1,0,1]The first update gives centers [0.5,0] and [9.5,10]. The second update leaves them unchanged. The final assignment keeps the two points near each center together.
Example 2
points = [[4,0],[4,0],[4,0],[6,0],[10,0]]initialCenters = [[0,0],[10,0]]iterations = 1return = [0,0,0,0,1]Using the starting centers, the first three points join cluster 0 and the last two join cluster 1. The update gives centers [4,0] and [8,0]. In the final assignment, [6,0] is equally distant from them, so it joins cluster 0.
Example 3
points = [[1],[3],[2]]initialCenters = [[0],[4],[4]]iterations = 0return = [0,1,0]No update is run, so each point goes straight to its nearest starting center. Point 1 chooses cluster 0. Point 3 is equally close to both copies of center 4, so it takes the lower-index copy. Point 2 is tied for distance, so it takes cluster 0, the smallest index.
Constraints
1 <= points.length <= 200.- The common dimension
dsatisfies1 <= d <= 10. 1 <= initialCenters.length <= min(points.length, 10).- Every row of both matrices has exactly
dcoordinates. - Every input coordinate is an integer in
[-1000,1000]. 0 <= iterations <= 50.
More NVIDIA Onsite Interview questions
- Check Endianness Of A Byte ArrayONSITE INTERVIEW · Seen Sep 2026
- Implement MallocONSITE INTERVIEW · Seen Sep 2026
- Implement PrintfONSITE INTERVIEW · Seen Sep 2026
- Merge Two Arrays Into Sorted OrderONSITE INTERVIEW · Seen Sep 2026
- Middle Node Of A Linked ListONSITE INTERVIEW · Seen Sep 2026
- Ping Reply TrackingONSITE INTERVIEW · Seen Sep 2026
- Reverse A StringONSITE INTERVIEW · Seen Sep 2026
- Maximum Fish in a GridONSITE INTERVIEW · Seen Sep 2024
- Minimum in a Rotated Sorted ArrayONSITE INTERVIEW · Seen Sep 2024
- First Register Overflow TickONSITE INTERVIEW · Seen May 2024
Similar questions at other companies
- Deterministic K-Means AssignmentsMeta · PHONE SCREEN · ONSITE INTERVIEWMeta interview questions
- Tensor-Parallel MLP GradientsOpenAI · ONSITE INTERVIEWOpenAI interview questions
- Transformer Attention and SwiGLU Forward PassGoogle · ONSITE INTERVIEWGoogle interview questions
- Deterministic K-Means AssignmentsMicrosoft · PHONE SCREEN · ONSITE INTERVIEWMicrosoft interview questions
- Vectorized K-Means AssignmentsSambaNova Systems · ONSITE INTERVIEWSambaNova Systems interview questions
- Expected Value of Modified DiceJane Street · ONSITE INTERVIEWJane Street interview questions
- Group-Relative Policy AdvantagesAmazon · ONSITE INTERVIEW
- Reservoir Sampling with Recorded DrawsStackAdapt · ONSITE INTERVIEWStackAdapt interview questions
- Moving Cost by Volume and CategoryWayfair · ONSITE INTERVIEWWayfair interview questions
- Sliding-Window Averages as Reduced FractionsCoinbase · ONSITE INTERVIEWCoinbase interview questions