Skip to content

Clustering and Dimensionality Reduction Interview Questions for Machine Learning

25 Clustering and Dimensionality Reduction questions with answers for machine learning roles — 3 easy, 15 medium, 7 hard.

About Clustering and Dimensionality Reduction

Unsupervised learning finds structure without labels. Learn k-means, choosing k, DBSCAN, hierarchical clustering, Gaussian mixtures, PCA, t-SNE and UMAP, anomaly detection with Isolation Forest and LOF, association rules and similarity search. Questions focus on customer segmentation, fraud screening without labels and turning clusters into segments business teams can act on.

Sheet progressStart practicing
3 QUESTIONS FREESign up to unlock the full question bank and track your progress.Unlock every question and answer, then continue practising company-wise and package-wise.
3 questions free Unlock the rest in seconds
01

How does k-means clustering work?

Save to ▾
Show answer
k-means picks k initial centroids, assigns each point to its nearest centroid, recomputes each centroid as the mean of its points, and repeats until assignments stop changing. It minimises within-cluster sum of squares (inertia). It needs k in advance, scaled features, and assumes roughly spherical, similar-sized clusters. k-means++ initialisation and multiple runs with n_init reduce bad starting points.
02

How do you choose the number of clusters k?

Save to ▾
Show answer
Common methods are the elbow method, plotting inertia against k and looking for the bend; the silhouette score, which measures how close points are to their own cluster versus the next one; and the gap statistic. Business usability matters too: four clear segments a marketing team can act on are better than nine statistically optimal but confusing ones.
03

Run k-means and plot the elbow and silhouette scores.

Save to ▾
Show answer
from sklearn.cluster import KMeans from sklearn.metrics import silhouette_score Xs = StandardScaler().fit_transform(X) for k in range(2, 10): km = KMeans(n_clusters=k, n_init=10, random_state=42).fit(Xs) print(k, round(km.inertia_), round(silhouette_score(Xs, km.labels_), 3)) Look for the bend in inertia and a high silhouette value.
04

What is DBSCAN and when is it better than k-means?

Save to ▾
Show answer
DBSCAN groups points that lie in dense regions, defined by eps (neighbourhood radius) and min_samples, and labels points in sparse areas as noise. It finds arbitrarily shaped clusters, does not need k, and handles outliers. It struggles when clusters have very different densities and in high dimensions. HDBSCAN improves on it for varying densities.
05

Explain hierarchical clustering and dendrograms.

Save to ▾
Show answer
Agglomerative hierarchical clustering starts with each point as its own cluster and repeatedly merges the two closest clusters. Linkage defines closeness: single, complete, average or Ward, which minimises variance. A dendrogram shows the merge history, and cutting it at a height gives the clusters. It does not need k upfront but scales poorly, roughly O(n squared) memory, so it suits smaller datasets.
06

What is PCA and how does it work?

Save to ▾
Show answer
Principal Component Analysis finds new orthogonal axes, the principal components, that capture maximum variance in the data. It works by computing eigenvectors of the covariance matrix, or via SVD, after centring and usually standardising the data. Keeping the top components that explain, say, 95 percent of variance reduces dimensions, removes correlation and speeds up models, but the components are harder to interpret.
07

How does Isolation Forest detect anomalies?

Save to ▾
Show answer
Isolation Forest builds random trees that split on random features and random thresholds. Anomalies are few and different, so they get isolated in fewer splits, giving shorter average path lengths. The anomaly score comes from that path length. It is fast, works without labels and scales well. The contamination parameter sets the expected share of anomalies and determines the threshold.
08

What is association rule mining? Explain support, confidence and lift.

Save to ▾
Show answer
Association rule mining finds items that occur together, such as in market basket analysis. Support is how often an itemset appears in all transactions. Confidence of A to B is the share of transactions with A that also contain B. Lift is confidence divided by B's support; lift above 1 means A and B occur together more than by chance. Apriori and FP-Growth are common algorithms.
09

Marketing wants customer segments for campaigns. How do you approach it?

Save to ▾
Show answer
I would agree on the purpose first, such as retention or upsell. Then build RFM features (recency, frequency, monetary) plus category preferences, log-transform skewed values and scale them. I would try k-means for k between 3 and 8, use silhouette and business review to pick k, profile each segment and give them clear names. Finally, test segment-based campaigns against a control group.
10

k-means gives one huge cluster and several tiny ones. What could be wrong?

Save to ▾
Show answer
Likely causes are unscaled features, where one large-range feature dominates distance; heavy skew or outliers pulling centroids; or data that is not naturally spherical. I would log-transform and scale features, remove or cap extreme outliers, and try different k values. If shapes are irregular, DBSCAN, HDBSCAN or a Gaussian Mixture Model may fit better.
11

Your dataset has 800 correlated features and the model trains slowly. What would you do?

Save to ▾
Show answer
I would remove constant and near-duplicate features, drop one from each highly correlated pair, then apply PCA inside the pipeline and keep components explaining about 95 percent of variance. Alternatively, use L1-regularised models or tree importances for supervised selection. I would compare speed and cross-validated scores; often 50 to 100 features give nearly the same accuracy.
12

Apply PCA and check explained variance.

Save to ▾
Show answer
from sklearn.decomposition import PCA import numpy as np pca = PCA().fit(Xs) cum = np.cumsum(pca.explained_variance_ratio_) n = np.argmax(cum >= 0.95) + 1 print(n, 'components explain 95 percent') X_red = PCA(n_components=0.95).fit_transform(Xs) Passing a float to n_components keeps enough components to reach that variance.
13

Detect anomalies with Isolation Forest.

Save to ▾
Show answer
from sklearn.ensemble import IsolationForest iso = IsolationForest(n_estimators=300, contamination=0.01, random_state=42) iso.fit(X_train) scores = -iso.score_samples(X_new) flags = iso.predict(X_new) == -1 Higher scores mean more anomalous. Tune contamination to match how many alerts reviewers can handle.
14

Build RFM features for customer segmentation in pandas.

Save to ▾
Show answer
snap = df['order_date'].max() + pd.Timedelta(days=1) rfm = df.groupby('customer_id').agg( recency=('order_date', lambda d: (snap - d.max()).days), frequency=('order_id', 'nunique'), monetary=('amount', 'sum')) rfm[['frequency', 'monetary']] = np.log1p(rfm[['frequency', 'monetary']]) Scale these columns before clustering.
15

Tell me about a time your analysis was not used by stakeholders. What did you learn?

Save to ▾
Show answer
Situation: I built eight customer segments, but marketing did not use them. Task: understand why. Action: I met the team and learned the segments were hard to act on and lacked clear names. I reduced them to five, added a one-page profile and suggested campaign ideas for each. Result: two segments were used in the next campaign. I learned to involve users from the start.
16

How do you prioritise when several teams ask you for models at the same time?

Save to ▾
Show answer
I estimate each request's business value, effort and urgency, then share a ranked list with my manager so priorities are agreed rather than assumed. For quick wins I offer a simple baseline first. In one quarter this helped us deliver a high-value churn model first while giving another team a dashboard-based interim answer, and both teams knew the timeline.
17

A product team wants 'similar products' recommendations with no purchase labels. How would you start?

Save to ▾
Show answer
I would build product embeddings from content: TF-IDF or sentence embeddings of titles and descriptions, plus category and price features. Then use cosine similarity with nearest-neighbour search, such as scikit-learn NearestNeighbors or an approximate index like FAISS for large catalogues. I would review samples with the product team and later add co-view or co-purchase signals once collected.
18

Find the most similar items using cosine similarity.

Save to ▾
Show answer
from sklearn.feature_extraction.text import TfidfVectorizer from sklearn.neighbors import NearestNeighbors X = TfidfVectorizer(min_df=2).fit_transform(df['title']) nn = NearestNeighbors(n_neighbors=6, metric='cosine').fit(X) dist, idx = nn.kneighbors(X[0]) print(df['title'].iloc[idx[0][1:]].tolist()) The first neighbour is the item itself, so skip it.
19

What is the difference between PCA and t-SNE or UMAP?

Save to ▾
Show answer
PCA is a linear method that preserves global variance, is fast, deterministic and can transform new data, so it is used for preprocessing. t-SNE and UMAP are non-linear methods that preserve local neighbourhoods, producing good 2D visualisations of clusters. t-SNE distances between clusters are not meaningful. UMAP is faster, keeps more global structure and can transform new points.
20

What is a Gaussian Mixture Model and how is it different from k-means?

Save to ▾
Show answer
A GMM assumes data comes from a mix of Gaussian distributions and fits their means, covariances and weights with the Expectation-Maximisation algorithm. Unlike k-means' hard assignments, GMM gives soft probabilities of belonging to each cluster and can model elliptical clusters of different sizes. You can choose the number of components using BIC or AIC.
21

How do you evaluate clustering when there are no labels?

Save to ▾
Show answer
Use internal metrics: silhouette score, Davies-Bouldin index (lower is better) and Calinski-Harabasz index (higher is better). Check stability by re-clustering on bootstrap samples. Most important is external usefulness: profile each cluster with key statistics and check whether business teams find the segments distinct and actionable, for example different response to a campaign.
22

You need to detect unusual transactions but have no fraud labels. What do you do?

Save to ▾
Show answer
I would start with unsupervised anomaly detection: Isolation Forest or Local Outlier Factor on features like amount relative to the customer's usual spend, time of day, new device and merchant risk. I would set the threshold based on what the review team can handle, collect investigator feedback as labels, and move to a supervised model once enough labelled cases exist.
23

The segments you built six months ago no longer match customer behaviour. What do you do?

Save to ▾
Show answer
I would measure drift by comparing feature distributions and segment sizes over time. Then refit clustering on recent data and map new clusters to old ones by centroid similarity so business names stay consistent. I would set up a quarterly refresh with stability checks and alert when segment sizes shift sharply, and explain any renamed or merged segments to stakeholders.
24

Run DBSCAN and count clusters and noise points.

Save to ▾
Show answer
from sklearn.cluster import DBSCAN import numpy as np db = DBSCAN(eps=0.5, min_samples=10).fit(Xs) labels = db.labels_ n_clusters = len(set(labels)) - (1 if -1 in labels else 0) print(n_clusters, 'clusters,', np.sum(labels == -1), 'noise') Pick eps from the knee of a sorted k-nearest-neighbour distance plot.
25

What is Local Outlier Factor and how does it differ from Isolation Forest?

Save to ▾
Show answer
Local Outlier Factor compares the local density of a point with the density of its neighbours; a point in a much sparser area than its neighbours gets a high outlier score. It catches local anomalies inside varied-density data but is distance-based, so it needs scaling and is slower on large data. Isolation Forest is tree-based, faster and scales better, but is less sensitive to local density differences.

Frequently asked questions

How important is Clustering and Dimensionality Reduction in Machine Learning interviews?

Unsupervised learning finds structure without labels. Learn k-means, choosing k, DBSCAN, hierarchical clustering, Gaussian mixtures, PCA, t-SNE and UMAP, anomaly detection with Isolation Forest and LOF, association rules and similarity search. Questions focus on customer segmentation, fraud screening without labels and turning clusters into segments business teams can act on.

How should I use the Clustering and Dimensionality Reduction sheet?

Go top to bottom — easy to hard. Answer before opening each answer, tick what you solved and save misses to Last-Minute Revision.