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?
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):
| Quantity | Value |
|---|---|
| n·k·d per iteration | 5×10⁷ × 20 × 40 = 4 × 10¹⁰ |
| × 30 iterations | 1.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.