Contents
11Interview questions and model answers
Q1How does K-means work?
It minimizes the within-cluster sum of squares J=\sum_i\|x_i-\mu_{c_i}\|^2 by alternating two exact minimizations: assign each point to its nearest centre, then move each centre to the mean of its points. Neither step increases J, and there are finitely many partitions, so it converges, but to a local minimum. Each iteration costs O(nKd). Use k-means++ seeding and several restarts, and standardize the features first.
Q2How do you choose K?
Plot J against K and look for the elbow; maximize the mean silhouette s=(b-a)/\max(a,b); use the gap statistic, which compares \log J_K with uniform reference data and can answer K=1; for mixtures use BIC. Often the application decides (for example, the number of campaigns a marketing team can run). J itself cannot be used, because it always decreases with K.
Q3What is k-means++ and why does it help?
A seeding rule: pick the first centre uniformly from the data, then each next centre with probability proportional to its squared distance D(x)^2 to the nearest centre already chosen. The centres spread out, which avoids two centres in one cluster, and the expected objective is within 8(\ln K+2) of the optimum even before Lloyd’s iterations. Its weakness is a pull toward outliers.
Q4When does K-means fail, and what would you use instead?
On elongated or unequal clusters (use a Gaussian mixture with full covariances), on non-convex shapes such as moons or rings (DBSCAN, single linkage, spectral clustering), with many outliers (DBSCAN, or K-medoids/K-medians), and with categorical features (K-modes or a suitable dissimilarity). The reason is that K-means uses squared distance to one centre per cluster, so its clusters are convex Voronoi cells.
Q5Explain the EM algorithm for a Gaussian mixture.
The E step computes responsibilities \gamma_{ik}\propto\pi_k\mathcal N(x_i\mid\mu_k,\Sigma_k), the posterior probability that point i came from component k. The M step re-estimates each component’s weight, mean and covariance as responsibility-weighted averages. Each iteration maximizes a lower bound on the log-likelihood that is tight at the current parameters, so the likelihood never decreases; it converges to a local maximum.
Q6How are K-means and Gaussian mixtures related?
K-means is the limit of EM for a mixture with equal spherical covariances \epsilon I as \epsilon\to0: the responsibilities become hard nearest-centre assignments and the expected log-likelihood becomes -J/(2\epsilon) plus a constant. A GMM adds soft assignments, cluster shapes (covariances) and weights, at the cost of more parameters.
Q7How does DBSCAN work, and what are its pros and cons?
A point with at least minPts points within distance \varepsilon is a core point; clusters are groups of core points connected through \varepsilon-neighbourhoods, plus the border points within reach of them; everything else is noise. Pros: any shape, no K, explicit outliers, deterministic for core points. Cons: sensitive to \varepsilon and minPts, struggles with clusters of different densities (HDBSCAN fixes this) and with high dimensions, where distances concentrate.
Q8Compare single, complete, average and Ward linkage.
Single uses the closest pair of points and follows chains, so it finds curved clusters but suffers from chaining; complete uses the farthest pair and gives compact clusters of small diameter; average is in between; Ward merges the pair that least increases the within-cluster sum of squares, \frac{|G||H|}{|G|+|H|}\|\bar x_G-\bar x_H\|^2, and behaves like K-means. The dendrogram shows every level; cut it where the gaps are largest.
Q9What is spectral clustering, and why do eigenvectors help?
Build a similarity graph, compute the eigenvectors of the graph Laplacian with the smallest eigenvalues, and run K-means on the points’ coordinates in those eigenvectors. f^\top Lf=\frac12\sum w_{ij}(f_i-f_j)^2 is small for vectors that are nearly constant on well-connected pieces; with m disconnected components there are m zero eigenvalues with indicator eigenvectors. The method is a relaxation of the (normalized) graph cut, and it handles any connected shape, but depends on the graph.
Q10How do you evaluate a clustering without labels? With labels?
Without labels: internal scores such as the silhouette or the within-cluster sum of squares (only comparable for the same K and data), stability under resampling and restarts, and above all whether the clusters are useful and interpretable for the task. With labels: external, permutation-invariant scores such as the adjusted Rand index or normalized mutual information. A clustering that matches the labels is not automatically the “right” one, since the labels may reflect a different notion of similarity.