# Unsupervised learning: clustering, PCA and anomaly detection

> What interviewers check on Unsupervised learning, and what to study, in order.

Canonical: https://saadho.arushk.tech/topics/unsupervised-learning
Last updated: 2026-10-26

ML fundamentals. Asked often. About 2 h 30 min to learn. 3 vetted resources.

## What this topic covers

Finding structure in data without labels. K-means splits points into k clusters by alternating assignments and centroid updates; it needs k chosen in advance, depends on its starting centroids and assumes compact clusters of similar size. Hierarchical clustering builds a tree of merges that you can cut at any level, and PCA finds the orthogonal directions of greatest variance for compression, visualisation or denoising. Anomaly detection flags points that fit the rest of the data poorly, with methods such as isolation forests or local outlier factor. Interviewers check that you can walk through k-means, choose k and a distance measure, explain why features must be scaled before k-means or PCA, and pick an anomaly detection approach when labels are scarce.

### Builds on

- [Probability and statistics for ML interviews](https://saadho.arushk.tech/topics/probability-stats)

## You’re ready when you can

- Explain the first principal component as the direction of greatest variance, and how loadings and scores relate
- Use the proportion of variance explained to choose how many components to keep
- Explain why variables are usually scaled before PCA and clustering
- Walk through the k-means algorithm and explain why it finds only a local optimum
- Describe how a dendrogram is built and how the choice of linkage changes the clusters
- Name the centroid, density, distribution and hierarchical families of clustering and a case for each
- Choose z-scores, log transforms or quantiles to prepare a feature before measuring similarity
- Judge cluster quality without labels using cardinality, magnitude and downstream performance
- Pick k with the elbow of the loss curve and list k-means’ main weaknesses with a remedy for each
- Distinguish outlier detection from novelty detection and say what training data each assumes

## Unsupervised learning interview questions

**Walk me through k-means. How do you choose k, and when does it fail?**

- K-means alternates assigning each point to its nearest centroid and moving each centroid to the mean of its points, lowering within-cluster squared distance until it converges to a local optimum.
- Because the result depends on the starting centroids, run several initialisations or k-means++ and keep the best run; choose k from the elbow of the loss curve, silhouette scores or downstream usefulness.
- It assumes compact clusters of similar size and is pulled by outliers and unscaled features, so standardise first and consider DBSCAN, Gaussian mixtures or hierarchical clustering for other shapes.

**What does PCA compute, and what should you watch out for when you use it?**

- PCA finds orthogonal directions of maximum variance in centred data, the eigenvectors of the covariance matrix or right singular vectors of the data matrix, ordered by the variance each explains.
- Scale features first when their units differ, otherwise the leading components simply follow whichever feature has the largest numeric range rather than any shared structure in the data.
- Choose the number of components from the cumulative proportion of variance explained or a scree plot, and remember that high-variance directions are not necessarily the ones that predict the label.

## What to study, in order

### 1. An Introduction to Statistical Learning

Start here. statlearning.com, Gareth James, Daniela Witten, Trevor Hastie, Robert Tibshirani and Jonathan Taylor. Guide, about 1 h 30 min. Free. Last checked 2026-10-26. [Open](https://www.statlearning.com/)

- **Why:** It explains PCA, k-means and hierarchical clustering with both the intuition and the maths interviewers expect, in one short chapter.
- **Read:** Download the free PDF of the Python edition (ISLP) from the home page. In chapter 12 read “12.1 The Challenge of Unsupervised Learning”, “12.2.1 What Are Principal Components?” to “12.2.4 More on PCA”, then “12.4.1 K-Means Clustering”, “12.4.2 Hierarchical Clustering” and “12.4.3 Practical Issues in Clustering”.
- **Skip:** “12.2.5 Other Uses for Principal Components”, “12.3 Missing Values and Matrix Completion”, and the lab unless you want to practise in code.

### 2. Introduction to clustering

Free alternative. Google for Developers, Google for Developers. Course, about 60 min. Free. Last checked 2026-10-26. [Open](https://developers.google.com/machine-learning/clustering)

- **Why:** It treats clustering as a practical workflow, from preparing data to judging the result, which is how interviewers pose it in product settings.
- **Read:** In the Overview unit read “What is clustering?”, “Clustering algorithms”, “Clustering workflow” and “Data preparation”; then the whole K-means clustering unit: “What is k-means clustering?”, “Manual similarity measure”, “Evaluating results” and “Advantages and disadvantages”.
- **Skip:** The Neural nets for clustering unit, unless you are also studying embedding-based similarity.

### 3. 2.7. Novelty and Outlier Detection

Go deeper. scikit-learn User Guide, scikit-learn developers. Guide, about 30 min. Free. Last checked 2026-10-26. [Open](https://scikit-learn.org/stable/modules/outlier_detection.html)

- **Why:** Anomaly detection questions usually come with few or no labels, and this page sets out the main unsupervised detectors and when each one fits.
- **Read:** Read the introduction on outlier versus novelty detection, “2.7.1. Overview of outlier detection methods”, the opening of “2.7.2. Novelty Detection”, “2.7.3.2. Isolation Forest” and “2.7.3.3. Local Outlier Factor”.
- **Skip:** “2.7.2.1. Scaling up the One-Class SVM”, most of “2.7.3.1. Fitting an elliptic envelope”, “2.7.4. Novelty detection with Local Outlier Factor” and the plotting code.

## Companies that test this area most

- [Google DeepMind](https://saadho.arushk.tech/companies/google-deepmind)
- [Flipkart](https://saadho.arushk.tech/companies/flipkart)
- [Sarvam AI](https://saadho.arushk.tech/companies/sarvam-ai)
- [Krutrim](https://saadho.arushk.tech/companies/krutrim)
- [Info Edge (Naukri)](https://saadho.arushk.tech/companies/info-edge)
- [Amazon](https://saadho.arushk.tech/companies/amazon)

## Related

- [All topics](https://saadho.arushk.tech/library)
- [Build a plan that includes this](https://saadho.arushk.tech/start)
