The interview problem

Practice prompt

A matrix has singular values 8, 4, 2 and 1. Choose the smallest rank retaining at least 95% of its squared Frobenius norm. Compute its reconstruction error, then explain whether the same rule safely compresses a neural-network weight matrix.

Original ML-fundamentals practice, not a reported company interview. Examples are hypothetical. Primary documentation retrieved September 27, 2026; interview date not applicable.

1. Specify the matrix and objective

For a real m×n matrix A, let q=min(m,n). Its reduced singular value decomposition is A=UΣVᵀ, with U of shape m×q, Vᵀ of shape q×n, and nonnegative singular values σ₁≥…≥σq on the diagonal of Σ. Columns of U and V are orthonormal. Complex matrices use conjugate transpose instead.

The rank-k truncation Aₖ=Σᵢ₌₁ᵏ σᵢuᵢvᵢᵀ keeps the leading directions. Among matrices of rank at most k, it minimizes both Frobenius reconstruction error and spectral reconstruction error. This statement concerns an unweighted matrix norm; it does not automatically optimize classification accuracy, a weighted loss or observed entries of an incomplete matrix.

2. Explain the residual rather than memorizing energy

The outer products uᵢvᵢᵀ are orthonormal under the Frobenius inner product. Consequently, the squared norm of their weighted sum is the sum of squared coefficients: ‖A‖²F=Σσᵢ² and ‖A−Aₖ‖²F=Σᵢ>k σᵢ². The residual's spectral norm is σk+1 when k<q, and zero when all nonzero singular directions are retained.

For the spectral lower bound, any rank-at-most-k candidate B has a null direction in the (k+1)-dimensional span of the leading right singular vectors. Along a unit vector in that intersection, ‖(A−B)x‖=‖Ax‖≥σk+1. Truncation attains that bound. For Frobenius error, projecting onto a k-dimensional output subspace can capture at most the sum of the leading k squared singular values; choosing the leading left singular vectors attains it.

A tie at the truncation boundary can give multiple optimal approximations. Singular-vector signs are arbitrary, and bases can rotate within repeated singular-value subspaces. Compare reconstructed matrices or subspaces rather than treating differing vector signs as an implementation bug.

3. Work the rank-selection example

Total squared energy is 8²+4²+2²+1²=85. Rank one retains 64/85≈75.29%; rank two retains 80/85≈94.12%; rank three retains 84/85≈98.82%. Therefore the smallest rank meeting 95% is three.

At rank two, squared Frobenius residual is 5, the Frobenius residual is √5≈2.236, relative Frobenius residual is √(5/85)≈24.25%, and spectral residual is 2. Retaining 94.12% of squared energy does not mean a 5.88% relative norm error: the latter requires a square root.

At rank three, squared residual is 1, Frobenius residual is 1, relative Frobenius residual is 1/√85≈10.85%, and spectral residual is 1. A requirement of at most 5% relative Frobenius error instead demands at least 99.75% squared energy, since 1−0.05²=0.9975. This example would require rank four.

For a zero matrix, the retained-energy ratio is undefined because its denominator is zero. Handle it explicitly as an exact rank-zero approximation. Numerical rank also needs a tolerance tied to precision and scale; a tiny computed singular value is not automatically meaningful signal.

4. Translate rank into storage and execution

Absorb singular values into one factor: L=UₖΣₖ and R=Vₖᵀ, so Aₖ=LR. Storage is k(m+n) scalars rather than mn, excluding metadata and any unchanged bias. Compression requires k<mn/(m+n). Keeping all reduced-SVD factors does not necessarily save memory.

For a 4096×4096 weight matrix at rank 256, dense storage is 16,777,216 scalars and factor storage is 2,097,152: eight times fewer. At two bytes per scalar these are 32 MiB and 4 MiB. For y=Ax, use z=Rx then y=Lz, with a 256-dimensional intermediate. The nominal multiplication count falls by the same factor, but two kernels, intermediate traffic, hardware utilization and batch size determine actual latency. Benchmark the real serving path before promising an eightfold speedup.

Factor quantization introduces additional approximation error, and materializing LR at runtime loses the compressed-storage advantage. SVD computation itself has cost; it may be an offline operation, not something to repeat on every request.

5. Show why weight energy is not task quality

Let E=A−Aₖ and let x be an input activation with second-moment matrix M=E[xxᵀ]. Expected squared output distortion is E[‖Ex‖²]=trace(E M Eᵀ). When M is the identity, this equals ‖E‖²F for every error matrix E. Other second moments can give equality for a particular E, but the unweighted norm does not generally represent output distortion. Centered activations use covariance, but uncentered activations require the full second moment, including the mean contribution.

Consider A=diag(8,1) with rank-one truncation diag(8,0). It retains 64/65≈98.46% of weight energy. Suppose inputs are always x=(0,10)ᵀ. True output is (0,10)ᵀ and compressed output is zero, so all output signal is lost despite high retained weight energy. An alternative rank-one matrix diag(0,1) reproduces these particular outputs exactly while having much worse unweighted matrix error.

Use representative training or calibration activations to choose candidates and evaluate the complete model on untouched data. Inspect rare slices, latency and memory together. Fine-tuning may recover quality but changes the artifact being evaluated. Do not select rank repeatedly against the final acceptance set.

6. Build an auditable approximation workflow

matrix + declared objective + preprocessing contract
                         ↓
          singular spectrum and candidate ranks
                         ↓
       reconstructed residual and factor-size checks
                         ↓
      held-out task quality + real hardware benchmark
                         ↓
           versioned factors and rollback artifact

For a small dense real matrix, NumPy returns U, s and Vh. With full_matrices=False, reconstruct a rank-k candidate as (U[:, :k] * s[:k]) @ Vh[:k, :]. NumPy's documentation defines these shapes and descending singular-value ordering. Check the residual numerically in appropriate precision rather than relying only on a rounded spectrum.

A randomized or iterative solver trades computation for approximation; its observed residual need not equal the optimal discarded-spectrum formula. Specify convergence tolerances, random seed and validation. Avoid explicitly forming AᵀA merely to obtain singular vectors when numerical conditioning matters: its condition number squares for a full-column-rank matrix under the spectral norm.

For PCA, center the training data before interpreting singular values as variance directions and apply the same learned centering to future data. For missing entries, zero filling changes the objective; ordinary truncated SVD is not automatically the solution to masked reconstruction. Feature scaling likewise changes which errors receive emphasis.

7. Follow-ups and self-review

Follow-ups: Is rank selection from 95% energy equivalent to 5% relative norm error? No. Is the optimal low-rank matrix unique when singular values tie? Not necessarily. Does an optimal matrix approximation optimize predictions under every input distribution? No. Does smaller factor storage imply proportionally faster serving? No.

Score 0–2 each for decomposition shapes, residual derivation, numerical rank selection, storage arithmetic and the activation-distribution counterexample. A strong answer names the norm being optimized and tests the actual downstream objective.

Sources and next steps

The derivations, rank-selection calculations and compression counterexample are original. Compare with quantization for a different compression mechanism and its error sources.

Continue your preparation

Choose a connected learning path or browse more ML fundamentals questions. Read our editorial method and attribution policy.