Vectorized K-Means Assignments
Updated
Problem statement
Implement K-Means clustering for integer points. Follow the steps below exactly, so the answer is the same every time. Use the first k points as the starting 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]. Write the arithmetic and the loop yourself; 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 SambaNova Systems Onsite Interview questions
- 4Sum II Tuple CountONSITE INTERVIEW · Seen Jun 2022
- Bomb Enemy Best CoordinatesONSITE INTERVIEW · Seen Jun 2022
- Sum Nodes with an Even-Valued GrandparentONSITE INTERVIEW · Seen Jun 2022
- Validate Binary Search TreeONSITE INTERVIEW · Seen Jun 2022
- Deterministic Zigzag SortPHONE SCREEN · ONSITE INTERVIEW · Seen Jun 2022
- Maximum Area of IslandONSITE INTERVIEW · 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
- Flip Equivalent Binary TreesONSITE INTERVIEW · Seen Oct 2021
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
- Fixed-Round K-Means AssignmentsNVIDIA · ONSITE INTERVIEWNVIDIA 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