FastPrepFixed-Round K-Means Assignments

Fixed-Round K-Means Assignments

NVIDIA logoNVIDIA● HardFULLTIMENEW GRADONSITE INTERVIEW
Learn

Problem statement

Implement k-means clustering directly for integer points. The rows of points are points in one common dimension. The rows of initialCenters are the supplied starting centers; their row indices are the cluster indices. Duplicate points and duplicate starting centers are valid.

Perform iterations Lloyd updates. Each update has these two phases:

  1. Assign every point to the old center with the smallest squared Euclidean distance, sum_a((point[a] - center[a])^2). Break an exact distance tie by choosing the smaller cluster index. Keep every old center fixed throughout this complete assignment pass.
  2. Replace each nonempty center with the coordinate-wise arithmetic mean of all its assigned point occurrences. If a cluster is empty, retain its previous center. Apply all replacements together.

After the last update, assign every point once more using the final centers and the same tie rule. Return one zero-based cluster index per input point, in input order. When iterations = 0, assign directly to the supplied starting centers.

Means and distance comparisons have their exact mathematical values; do not round or truncate centers and do not introduce an approximate tie tolerance. Implement the assignment and update arithmetic without a clustering library. The fixed update budget defines the result; convergence is not required.

What the interview report shared

The onsite report asks for handwritten k-means during a basic machine-learning and C++ interview. It does not disclose the data, initialization, stopping rule, empty-cluster behavior, ties, or return interface.

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 supplied 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 performed. Assign directly to the supplied centers: 1 chooses cluster 0, 3 chooses the lower-index copy of center 4, and 2 chooses cluster 0 on a distance tie.

Constraints

  • 1 <= points.length <= 200.
  • The common dimension d satisfies 1 <= d <= 10.
  • 1 <= initialCenters.length <= min(points.length, 10).
  • Every row of both matrices has exactly d coordinates.
  • Every input coordinate is an integer in [-1000,1000].
  • 0 <= iterations <= 50.

More NVIDIA problems

See NVIDIA hiring insights
public int[] fixedRoundKMeans(int[][] points, int[][] initialCenters, int iterations) {
    // Return one cluster index per point.
}
points[[0,0],[10,10],[1,0],[9,10]]
initialCenters[[0,0],[10,10]]
iterations2
expected[0,1,0,1]
Checking account…