Clustering interview question

Machine LearningClusteringML System Designmedium

A telecom wants k = 20 segments for 50 million subscribers described by 40 numeric features stored as float64. Full-batch k-means (Lloyd's algorithm) typically needs about 30 iterations on this data. Which estimate of the work and which scaling plan are correct?

  1. AEach iteration compares every pair of subscribers, about n²·d ≈ 10¹⁷ operations, so k-means is infeasible here; switch to agglomerative Ward clustering, which avoids that cost by merging nearest pairs.
  2. BEach iteration computes n·k·d = 4 × 10¹⁰ multiply-adds, about 1.2 × 10¹² over 30 iterations; switch to k-means++ seeding, which places the centroids in one pass so no Lloyd iterations are needed.
  3. CEach iteration costs n·d = 2 × 10⁹ multiply-adds, because centroids are updated once per pass whatever k is, so 6 × 10¹⁰ in all; one full-batch run on one machine is the best plan.
  4. DEach iteration computes n·k·d = 4 × 10¹⁰ multiply-adds, about 1.2 × 10¹² over 30 iterations; use MiniBatchKMeans, or fit on a few-million-row sample and then assign all rows in one pass.
Show the answer and explanation

Correct answer: D, Each iteration computes n·k·d = 4 × 10¹⁰ multiply-adds, about 1.2 × 10¹² over 30 iterations; use MiniBatchKMeans, or fit on a few-million-row sample and then assign all rows in one pass.

Explanation Clustering

One Lloyd iteration has two steps: assign every point to the nearest of k centroids (n·k distance computations over d dimensions) and recompute the centroids (n·d). The assignment step dominates, so the cost is O(n·k·d·iterations):

QuantityValue
n·k·d per iteration5×10⁷ × 20 × 40 = 4 × 10¹⁰
× 30 iterations1.2 × 10¹²
Data read per pass (float64)5×10⁷ × 40 × 8 bytes = 16 GB

The cost is linear in n, so k-means is feasible, but every iteration rereads 16 GB. Two standard ways to scale:

  • MiniBatchKMeans updates the centroids from small random batches (say 10,000 rows), so each step costs batch·k·d instead of n·k·d. It needs far fewer full passes over the data, usually at a small cost in inertia.
  • Fit on a sample, then assign. Centroids usually stabilise with a few thousand points per cluster, so fit on 2 million rows (2×10⁶ × 20 × 40 × 30 = 4.8 × 10¹⁰) and assign all 50 million once (4 × 10¹⁰): about 14 times less work, and the assignment pass parallelises trivially. Managed options such as BigQuery ML k-means exist as well.

Why the others fail: the n² pairwise cost belongs to agglomerative clustering (O(n²) memory and at least O(n²) time), which is the method that cannot scale here. k-means++ only chooses the starting centroids: its seeding makes k sequential passes over the data, and the Lloyd iterations still follow, usually fewer of them (scikit-learn's KMeans already uses it by default). Dropping k from the formula ignores that every point is compared with every centroid.

Interview tip: state the complexity O(n·k·d·i) and the data volume per pass, then propose sample-fit-assign or mini-batch; mention that distance computations vectorise as matrix products.

Practice it as a quiz

Answer this and the other free questions with instant grading, streaks and weak-topic tracking, or unlock all 4,000 questions.

More Machine Learning questions

  1. An e-commerce team trains pricing agents in a simulator and keeps a large buffer of transitions collected by older versions of its policies. Which grouping of…
  2. A telecom team tunes a churn network with successive halving: 81 configurations, a minimum budget of 1 epoch, a maximum of 81 epochs and η = 3 (keep the best…
  3. What is the difference between model parameters and hyperparameters?
  4. What does the bias–variance trade-off describe?
  5. What does the mean squared error cost J(w) = (1/n) Σ (yᵢ − ŷᵢ)² measure for a linear regression model?