Articles
Hierarchical Clustering Interview Questions: Linkage, Scaling, and Dendrogram Cuts
Practice hierarchical clustering interviews with linkage traces, Ward's objective, scaling counterexamples, dendrogram cuts, and checks for misleading groups.
Hierarchical clustering interview questions become easier when you separate three choices: the distance between observations, the rule for comparing groups, and where you cut the resulting tree. A dendrogram records merges under those choices. Its height is not automatically a maximum distance between every pair inside a cluster, and a visually appealing branch does not prove a natural population category.
This guide uses one four-point merge trace and one unit-conversion counterexample to make those distinctions concrete.
Evidence boundary: official SciPy and scikit-learn documentation establishes the library semantics below. The datasets, hand calculations, and suggested interview follow-ups are original preparation material.
Explain a merge before drawing a tree
Agglomerative clustering starts with each observation in a separate group, repeatedly merges a selected pair, and builds a hierarchy. The important follow-up is what makes that pair the selected pair. “They are closest” is incomplete until you specify how distance between groups is measured.
A hierarchy helps when the reader needs several resolutions of the same grouping: broad families, subgroups, and individual records. It does not make the grouping more objective than a flat partition. An early merge is retained in the ordinary agglomerative construction; later decisions do not relocate an observation to repair that earlier choice.
Our original example labels four one-dimensional observations A=0, B=2, C=3, and D=10. Euclidean distance on a line is the absolute difference. B and C are the closest pair, with distance 1, so they merge first under single, complete, and average linkage.
Now stop and ask the interviewer's next question: what is the distance from A to the new group {B,C}? That one calculation exposes most of the differences people hide behind a dendrogram picture. It also prevents confusing a linkage method with a feature-distance metric.
Compare single, complete and average linkage
Official definitions: SciPy documents single linkage as the minimum cross-group point distance, complete linkage as the maximum, and average linkage as the mean across all cross-group pairs. These definitions determine how group distances change after a merge. SciPy linkage
For A versus {B,C}, the point distances are 2 and 3. Single linkage gives 2, complete gives 3, and average gives 2.5. The distances from D to {B,C} are 8 and 7, yielding 7, 8, and 7.5 respectively. Therefore A joins {B,C} next under all three methods, but at different heights.
The final merge compares {A,B,C} with D. Single takes the smallest of 10, 8, and 7, giving 7. Complete takes the largest, giving 10. Average takes their mean, 25/3, approximately 8.333. The same first merge and final group do not imply the same intervening hierarchy.
| Linkage | Merge heights for A=0, B=2, C=3, D=10 | Groups at height 2.7 |
|---|---|---|
| Single | 1, 2, 7 | {A,B,C}, {D} |
| Average | 1, 2.5, 8.333 | {A,B,C}, {D} |
| Complete | 1, 3, 10 | {A}, {B,C}, {D} |
Our local SciPy 1.18.1 check reproduced these heights and the cut partitions. The fixture uses distinct relevant distances, so its intended comparisons do not depend on a tied first merge. The final average height is rounded in the table; the executable expectation uses the exact fraction 25/3 before floating-point comparison.
Why can a cut contain distant observations?
At height 2.7, single linkage puts A and C in one group even though their point distance is 3. Their connection runs through B: A–B is 2 and B–C is 1. A cut therefore bounds the edges needed to connect this group, not every within-group pair distance.
This is the mechanism behind chaining. Imagine a long path of observations with short gaps between consecutive points. The endpoints can be far apart while the path keeps extending a single-linkage group. Calling that a bug misunderstands the selected criterion; whether the result is useful depends on whether connectivity matches the task.
Average linkage also does not generally impose a maximum pairwise-distance bound. An average can remain small while one pair is far apart. In this example, A enters {B,C} at average height 2.5 despite its distance 3 from C.
Complete linkage directly considers the largest cross-group distance. For the ordinary complete-linkage hierarchy here, cutting at 2.7 leaves A outside {B,C}. Be precise about the claim: it concerns this distance and linkage construction, not every constrained or modified hierarchy someone could produce.
A good answer says, “I would choose the criterion that matches whether a cluster means a connected chain, a compact group, or an average similarity. I would not translate a cut height into a business rule until I verify what that height represents.”
What makes Ward linkage different?
Ward's method targets the increase in within-group squared error when merging groups. It is not simply average linkage with another name. For group sizes a and b, means mu_A and mu_B, the increase is a*b/(a+b) * ||mu_A-mu_B||² under Euclidean geometry.
Our first merged group {2,3} has size 2 and mean 2.5. Merging it with singleton 0 increases squared error by (2*1/3)*2.5² = 25/6, about 4.167. That is an increase in an objective, not the raw point distance from 0 to the nearest member.
Official implementation boundary: SciPy's Ward linkage height follows its specified distance convention, and Ward requires Euclidean distances. For this merge, the reported height is sqrt(2 * 25/6), approximately 2.887. Do not equate the plotted height with the squared-error increase without checking the convention. Ward definition and constraints
The practical mistake is to feed arbitrary dissimilarities into Ward because the array is accepted. A symmetric matrix with a zero diagonal does not establish Euclidean geometry. Either use compatible feature coordinates and metric or choose a linkage criterion suited to the supplied dissimilarities.
Show how units change the first merge
Consider a second original dataset: A=(0,0), B=(1,100), and C=(3,101). In raw Euclidean units, B–C is about 2.236, whereas A–B is about 100.005. B and C merge first. The second feature's numerical range dominates that comparison.
Now divide the second coordinate by 100. The points become A=(0,0), B=(1,1), and C=(3,1.01). A–B is about 1.414, while B–C is about 2.000. A and B now merge first. Nothing about the underlying records changed; the representation changed their geometry.
This example does not prove that the second scale is correct. Equal numerical ranges, unit variance, and domain-specific weights are different modeling decisions. A high-variance feature can contain useful structure; standardizing automatically can suppress a meaningful difference or magnify noisy features.
Explain the purpose first. If feature one measures a behavior rate and feature two measures accumulated spending, perhaps a time normalization or log transform is more defensible than matching ranges. Record the transform and verify its effects on actual neighbor pairs.
If clusters support decisions about future observations, fit learned preprocessing using the permitted training information and retain that transform for evaluation. A rescaled distance is reproducible only when the transformation parameters and feature definitions are also recorded.
Read the dendrogram without overclaiming
Official plotting semantics: SciPy's dendrogram uses linkage distances to position merges and offers truncation and leaf-order options. Leaves can be reordered for display without changing the underlying merges. A left-to-right sequence is not automatically a meaningful ranking of observations. SciPy dendrogram
Check the vertical axis and the method before interpreting a long branch. A large gap between successive merge heights can suggest a useful resolution to inspect, but its size depends on the representation and linkage. It is not a confidence interval or a significance test.
Truncated plots can also hide small groups. Ask whether a displayed leaf represents one observation or an already merged branch. Otherwise a large-looking “cluster” can be a rendering choice rather than an actual large group.
A hierarchy can exist for unstructured data because the algorithm is required to keep merging. Your interview explanation should separate the tree construction from the evidence that any cut deserves operational use. Profile the resulting groups and inspect outliers rather than simply naming them from branch colors.
Distinguish a height threshold from a cluster-count request
Official API fact: SciPy's fcluster supports different cut criteria. A distance cut and a maxclust request answer different questions; the latter seeks no more than the requested number under its threshold rule. Tied merge heights can make “exactly K” an unsafe expectation. SciPy fcluster
For our four-point tree, specify criterion='distance' and threshold 2.7 to reproduce the ledger. If you leave the criterion implicit and it means inconsistency, the number 2.7 no longer expresses the same request. Matching a parameter's value while changing its semantics is not a reproduction.
The scikit-learn interface also distinguishes a requested group count from a distance threshold and exposes connectivity restrictions. A restriction can prevent otherwise eligible merges, so it changes the permitted hierarchy rather than merely speeding up the same computation. AgglomerativeClustering API
State what to do at an exact boundary. In a hand exercise, “cut at the first merge height” requires an inclusive or exclusive convention. Read the library's actual threshold semantics; do not assume its picture has settled that convention for you.
How would you validate the hierarchy?
Start with a tiny known dataset and reconstruct at least one post-merge distance manually. Then check that the linkage matrix represents valid children, finite heights, and group counts. A library call that returns an array is not enough if the input represented the wrong object.
In SciPy, a square distance matrix passed as a two-dimensional observation array can be interpreted as rows of features. If you intend precomputed distances, use the documented condensed representation with validation. The resulting tree can otherwise look plausible while clustering distance profiles instead of the original observations.
For real data, compare reasonable transformations and linkage choices, inspect small and oversized groups, and check membership under resampling or measurement perturbations. Align group membership rather than comparing arbitrary label numbers. Ties can expose implementation or input-order choices, so preserve identifiers and report the convention.
Consider compute limits before promising a full pairwise hierarchy. A condensed float64 distance vector for 100,000 observations contains 4,999,950,000 entries, nearly 40GB before other working memory. Sparse or constrained approaches may help, but describe the changed assumptions and implementation rather than promising universal linear memory.
The final answer should connect the cut to a use: compact document families, connected components for review, or a nested taxonomy that domain experts can inspect. If no stable and useful resolution exists, explain that finding rather than forcing a segment count.
Five related questions to rehearse
These records are related practice. The linkage and cut exercises are original extensions; the linked prompts are not all reported hierarchical-clustering questions.
| Practice question | Hierarchy-specific extension |
|---|---|
| Which clustering algorithm would you use and why | Explain when nested resolutions justify a hierarchy. |
| Implement K-Means and compare with GMM | Contrast objective-based flat groups with irreversible merges. |
| Explain KNN and how to tune it | Reuse the explicit distance and scaling discussion. |
| How would you design delay and watchlist models? | Explain whether grouping correlated features preserves useful information. |
| Compare PCA and Autoencoders Under the Curse of Dimensionality | Check how compression changes distances before building the tree. |
Use the clustering-selection exercise, calculate the four-point trace under three linkages, and explain which pairwise statement the cut does—and does not—support.