Skip to content
Tech Interview Prep
Technical interview guideNew

Principal Component Analysis (PCA)

Linear dimensionality reduction that rotates features onto orthogonal directions of maximum variance, so a few components keep most of the information. Interviews probe the covariance and SVD derivation, scaling and leakage discipline, and what PCA cannot do.

Minutes to read
4
Practice MCQs
25
Interview Q&A
25
Edition
v2
Editorial status
Reviewed

Scope: Mathematics is version-independent. Code examples target scikit-learn 1.5 or later (sklearn.decomposition.PCA with svd_solver auto, full, arpack, randomized and covariance_eigen; whiten and n_components as documented). Default behaviour: centres features, does not scale them, and fits on the data passed to fit..

Overview

Curated: · Written: · Reviewed:

Principal Component Analysis (PCA)

In one line

PCA rotates centred data onto orthogonal directions of maximum variance, orders them by variance, and keeps the first k. Reach for it when features are numeric and linearly correlated, and you need a reusable linear reduction with a known reconstruction error.

Key definitions

  • Centred data: every column has mean zero. PCA is defined on centred data only.
  • Covariance: C = X^T X / (n-1). Symmetric and positive semidefinite, so its eigenvectors are orthogonal and its eigenvalues are non-negative.
  • Principal component: an eigenvector of C, ranked by its eigenvalue. Its sign is arbitrary.
  • Loadings: the eigenvector coefficients, as used here. Some sources mean feature-component correlations, so check which before comparing numbers.
  • Explained variance ratio: lambda_i divided by the sum of all eigenvalues.
  • Whitening: dividing each score by the square root of its eigenvalue, which gives the scores identity covariance.

Formulas and complexity

C=X⊤Xn−1,Cvi=λivi,Var(zi)=λiC = \frac{X^\top X}{n-1}, \quad C v_i = \lambda_i v_i, \quad \mathrm{Var}(z_i) = \lambda_i λi=σi2n−1\lambda_i = \frac{\sigma_i^2}{n-1} ∑s=1n∥xs−x^s∥2=(n−1)∑i>kλi(training rows, divisor n−1)\sum_{s=1}^{n} \lVert x_s - \hat{x}_s \rVert^2 = (n-1)\sum_{i>k}\lambda_i \quad \text{(training rows, divisor } n-1\text{)}

Full SVD of an n by d matrix costs about O(n d^2) when n is at least d. Truncated or randomised SVD computes only the top k components, which suits large d with small k.

How it works

Centre the features with training means. Take the SVD of the centred matrix, X = U S V^T, or eigendecompose C. Sort by variance and keep the first k columns of V. Scores are Z = X V_k. The reconstruction is the orthogonal projection onto the k-dimensional subspace, plus the mean. The discarded components carry the residual variance, and that residual is the reconstruction error.

Raw X, n by d Centre with training means SVD of centred X Keep top k columns of V Scores Z = X V_k Downstream model Reconstruction X_hat

When to use and when not to

SituationPCA fits?Use instead
Numeric, linearly correlated features with mixed unitsYes, after standardisingNone needed
Curved structure such as concentric ringsNo, it is linearKernel PCA or an autoencoder
Labels exist and you need class separationWeak, variance ignores labelsLDA or supervised selection
Model must explain predictions in original columnsNo, components are mixturesFeature selection
One-hot categorical columnsPoor, sparse correlated blocksMultiple correspondence analysis
Visual exploration of clustersBlurs clusterst-SNE or UMAP, not as model inputs

Traps interviewers probe

  • Fitting the scaler or PCA before the split. The validation score comes out optimistic, and nothing crashes to warn you. Fit both on training rows only, then transform test rows with the fitted objects. In cross-validation, put them inside the pipeline so they refit per fold.
  • Refitting the transforms on a new batch. A new reading must be centred and scaled with training means and standard deviations, then projected with the stored loadings. Refitting on the batch moves the components and breaks any downstream weights.
  • Treating variance as usefulness. The top components keep the most spread, not the most signal for your target. Choose k with a validation metric, and use the cumulative ratio as a secondary check.
  • Reading signs and axes as meaning. Flipped signs are normal across fits. Align a new component to the stored one before comparing or feeding a fixed weight. Do not name a component from its loadings without a check.
  • Equating discarded variance with reconstruction error. On training rows with divisor n-1, the total squared error is (n-1) times the sum of discarded eigenvalues, not the bare sum.

Interview tips

Open with the assumptions: linear, variance-based, centred data. Then give the link: the top eigenvector of C maximises v^T C v under unit length, and the maximum is its eigenvalue. Name the SVD route and say why it avoids squaring the condition number.

Expect these follow-ups:

  • Why are components orthogonal? Because C is symmetric, and its eigenvectors with distinct eigenvalues are orthogonal.
  • Why is PCA the best linear compression? It minimises total squared reconstruction error among rank-k linear projections.
  • Why standardise? Variance is measured in each feature's own units, so a large unit dominates.
  • What goes wrong if you fit before the split? The test estimate is optimistic.
  • When does whitening hurt? It amplifies small components, which are often noise.

30-second outline: PCA is a linear, unsupervised rotation onto the directions of greatest variance, computed from the SVD of the centred data. I standardise when units differ, fit on training rows only, and choose k with a validation metric, using cumulative variance as a guide. Reconstruction error is the discarded eigenvalues, scaled by n-1 on training rows. It fails on curved, categorical or label-driven structure, and there I would reach for kernel PCA, encoding or LDA.

Feedback on this overview

Log in free to save your progress and keep bookmarks and notes for a quick revision before your interview.