← Two Sigma Interview Insights

Two Sigma·Data Scientist·Technical Phone Screen·Senior

Senior
Jul 2026

Summary

Two Sigma data scientist interview that was essentially a deep OLS regression question dressed up as a coding exercise. Five sub-parts, all interconnected, and it moved fast.

Questions Asked (5)

Q1

You're given two separate dataframes, one with features (user_id, clicks, impressions) and one with a target (user_id, conversions). Inner-join them on user_id, then fit a no-intercept OLS model using both the normal equations and a numerically stable factorization like QR or SVD. Show the intermediate matrices XᵀX and Xᵀy and the final coefficient vector.

Data ModelingAlgorithms & Data StructuresTechnical Trade-offs
Author's notes

This part took longer than I expected because I kept second-guessing the join step.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clearly outlining the data preparation steps: inner-join the two dataframes on user_id, handle any missing values, and construct the design matrix X with a column of ones for the intercept (or explicitly no intercept). Then explain the two solution methods: normal equations (XᵀX)⁻¹Xᵀy and a numerically stable approach like QR or SVD, highlighting the trade-offs. Finally, compute and present the intermediate matrices XᵀX, Xᵀy, and the coefficient vector, discussing numerical stability and potential pitfalls.

Pro tip: Emphasize that while the normal equations are mathematically straightforward, they can be numerically unstable due to squaring the condition number; using QR or SVD is preferred in practice, especially with large or ill-conditioned data. Also, mention that for a no-intercept model, you should not include a column of ones in X, and ensure the design matrix is full rank.

1. Data Preparation and Join

Perform an inner join on user_id to combine features and target, ensuring only users present in both dataframes are kept. Handle any missing or infinite values appropriately.

2. Construct Design Matrix and Target Vector

Build the design matrix X from the feature columns (clicks, impressions) without adding an intercept column, and the target vector y from the conversions column.

3. Compute Normal Equations Solution

Calculate XᵀX and Xᵀy, then solve for coefficients using the normal equations: β = (XᵀX)⁻¹Xᵀy. Show these intermediate matrices.

4. Implement Numerically Stable Factorization

Use QR decomposition (X = QR) or SVD (X = UΣVᵀ) to solve for β without explicitly forming XᵀX. For QR, β = R⁻¹Qᵀy; for SVD, β = VΣ⁻¹Uᵀy.

5. Compare and Present Final Coefficients

Compare the coefficient vectors from both methods, discuss any differences due to numerical stability, and present the final coefficient vector.

Key Points to Mention

  • Inner join ensures only users with both features and target are used, avoiding missing data issues.
  • No-intercept model means X does not include a column of ones; coefficients represent direct effects.
  • Normal equations involve computing XᵀX and Xᵀy, but can be unstable if X is ill-conditioned.
  • QR decomposition solves via back substitution and is more stable than normal equations.
  • SVD is the most stable but computationally heavier; it handles rank deficiency well.
  • Always check for multicollinearity and condition number of XᵀX to assess stability.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q2

Compute R-squared for the no-intercept model. The correct TSS here uses sum of squared y values, not sum of squared deviations from the mean. Explain why this version of R-squared can go negative and how it compares to the standard intercept model's R-squared.

Data ModelingProduct Analytics & Metrics
Author's notes

I got the formula wrong the first time.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by writing the formula for R-squared in the no-intercept model, emphasizing that TSS is the sum of squared y values (not deviations from the mean). Then explain that because the model does not include an intercept, the residuals are not orthogonal to the fitted values, and the model can fit worse than a constant zero predictor, leading to negative R-squared. Finally, compare this to the standard intercept model where R-squared is always between 0 and 1 due to the orthogonality and the mean-centering of TSS.

Pro tip: Mention that negative R-squared in the no-intercept model is a sign that the model is performing worse than a naive zero predictor, and that some software (like R) automatically reports this version, so it's important to be aware of the difference when interpreting results.

1. Define R-squared for no-intercept model

Write the formula: R² = 1 - (RSS / TSS), where RSS = Σ(y_i - ŷ_i)² and TSS = Σ y_i². Clarify that TSS is not mean-centered.

2. Explain why R-squared can be negative

Since TSS is the sum of squared y values, it represents the error of predicting zero for all observations. If the model's predictions are worse than predicting zero, RSS > TSS, so R² < 0.

3. Contrast with standard intercept model

In the intercept model, TSS is Σ(y_i - ȳ)², and the model includes a constant, so the mean of residuals is zero and the regression line is orthogonal to residuals. This guarantees R² ≥ 0.

4. Discuss implications and interpretation

Negative R² indicates the no-intercept model fits worse than a zero predictor. It is not a flaw but a consequence of the definition; it highlights that the model may be inappropriate.

Key Points to Mention

  • TSS definition: sum of squared y values vs. sum of squared deviations from mean
  • RSS = sum of squared residuals
  • Orthogonality condition: in intercept model, residuals sum to zero and are orthogonal to predictors
  • Negative R² means model performs worse than predicting zero
  • Software differences: R's lm() without intercept reports this R², while some other software may not
  • Comparison to intercept model: R² always between 0 and 1

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q3

When is it actually appropriate to drop the intercept from a regression model? Mean-center both X and y, then refit with and without an intercept and comment on what changes with the coefficients.

Data ModelingTechnical Trade-offs
Author's notes

Shorter answer than I thought they wanted.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

First, explain the theoretical and practical reasons for dropping the intercept, emphasizing that it should be rare and only when the regression is known to pass through the origin. Then, describe the mean-centering experiment: mean-center both X and y, refit with and without an intercept, and compare the coefficients, noting that without an intercept the slope is biased unless the true intercept is zero.

Pro tip: Mention that mean-centering makes the intercept interpretable as the expected value of y at the mean of X, and that dropping the intercept after centering forces the line through the origin, which is almost never justified unless theory dictates it.

1. When to drop the intercept

Discuss that dropping the intercept is appropriate only when theory or domain knowledge strongly implies the regression passes through the origin (e.g., physical laws) or when the model is reparameterized (e.g., with dummy variables for all categories).

2. Consequences of dropping the intercept

Explain that omitting the intercept forces the regression line through the origin, which can bias slope estimates and inflate R-squared if the true intercept is non-zero.

3. Mean-centering procedure

Describe mean-centering: subtract the mean from both X and y. This shifts the data so that the origin is at the means, making the intercept represent the expected y at the mean of X.

4. Refit with and without intercept

After centering, fit two models: one with an intercept and one without. Compare the coefficients, standard errors, and fit statistics.

5. Interpret changes

With intercept: the intercept should be near zero (since data is centered) and the slope is the same as the uncentered model. Without intercept: the slope is forced through the origin, which may change the slope estimate and typically reduces the intercept to zero, but can bias the slope if the true intercept is not zero.

Key Points to Mention

  • Dropping the intercept is rarely appropriate; it should be based on strong theoretical justification.
  • Mean-centering does not change the slope estimate when an intercept is included; it only changes the interpretation of the intercept.
  • Without an intercept, the slope estimate is biased unless the true intercept is exactly zero.
  • R-squared can be misleading when the intercept is dropped; use alternative measures like uncentered R-squared.
  • Mean-centering can reduce multicollinearity in interaction terms but does not justify dropping the intercept.
  • Always check residual plots and domain knowledge before considering dropping the intercept.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q4

If XᵀX is singular or near-singular due to collinearity between clicks and impressions, how do you detect that and what do you do? Derive the closed-form ridge regression estimator with regularization parameter lambda and compute it on the sample data.

Data ModelingTechnical Trade-offsAlgorithms & Data Structures
Author's notes

Collinearity detection I handled fine, talked about condition number and eigenvalues.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by explaining how to detect multicollinearity using condition number, VIF, or correlation matrix, and discuss its consequences. Then derive the ridge regression closed-form solution (XᵀX + λI)⁻¹Xᵀy, and finally compute it on the sample data, showing the steps clearly.

Pro tip: Mention that ridge regression introduces bias but reduces variance, and that λ should be chosen via cross-validation; also note that centering the data can help interpret the intercept.

1. Detect multicollinearity

Compute the condition number of XᵀX, variance inflation factors (VIF), or pairwise correlations. A condition number > 30 or VIF > 10 indicates severe multicollinearity.

2. Explain consequences

Discuss how multicollinearity inflates coefficient variance, makes estimates unstable, and can lead to overfitting. Mention that predictions may still be good but interpretation is unreliable.

3. Derive ridge estimator

Start from the ridge objective: minimize ||y - Xβ||² + λ||β||². Take derivative w.r.t. β, set to zero, and solve to get β_ridge = (XᵀX + λI)⁻¹Xᵀy.

4. Compute on sample data

Plug in the given X and y into the formula. If data is not provided, outline the computation steps: form XᵀX, add λI, invert, multiply by Xᵀy. Show intermediate results if possible.

5. Discuss λ selection

Mention that λ is a hyperparameter typically chosen via cross-validation. Larger λ increases bias but reduces variance. Also note that when λ=0, it reduces to OLS.

Key Points to Mention

  • Condition number and VIF as detection tools
  • Multicollinearity leads to high variance and unstable coefficients
  • Ridge regression adds L2 penalty to stabilize estimates
  • Closed-form solution: β_ridge = (XᵀX + λI)⁻¹Xᵀy
  • λ controls regularization strength; selected via cross-validation
  • Ridge does not perform variable selection (unlike Lasso)

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.

Q5

How would you validate this model in practice? Walk through residual diagnostics and cross-validation, and explain how you'd prevent data leakage when constructing the feature and target dataframes.

A/B Testing & ExperimentationData ModelingRoot Cause Analysis
Author's notes

The leakage question felt like a trap but I think I got it.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Structure your answer around a clear validation pipeline: first describe how you'd split data and construct features/targets without leakage, then explain cross-validation and residual diagnostics, and finally tie it back to practical decision-making. Emphasize that validation is not just a technical step but a way to build trust in the model's predictions.

Pro tip: Mention that you always simulate the production data pipeline when creating features, and use time-based splits if there's any temporal component—this shows you understand real-world deployment pitfalls. Also, highlight that residual diagnostics should be automated and monitored in production, not just done once.

1. Data Splitting and Leakage Prevention

Explain how you'd split data (e.g., time-based, group-based, or random) and ensure that feature engineering steps like scaling, imputation, or target encoding are fit only on training data. Emphasize that the target variable should never be used in feature construction unless it's a lagged version.

2. Cross-Validation Strategy

Describe the cross-validation scheme (e.g., k-fold, stratified, time-series split) and why it's appropriate for the data structure. Mention that you'd use nested CV if hyperparameter tuning is involved to avoid optimistic bias.

3. Residual Diagnostics

Walk through key residual plots: residuals vs. fitted values, QQ-plot, residuals vs. predictors, and autocorrelation of residuals. Explain what patterns you'd look for (e.g., heteroscedasticity, non-linearity, outliers) and how they'd inform model improvements.

4. Performance Metrics and Validation

Discuss appropriate metrics (e.g., RMSE, MAE, AUC) and how you'd compare them across folds. Mention the importance of confidence intervals or statistical tests to ensure differences are significant.

5. Production Monitoring and Iteration

Explain how you'd monitor residuals and performance in production, set up alerts for drift, and iterate on the model. This shows you think beyond one-time validation.

Key Points to Mention

  • Data leakage prevention: fit preprocessing only on training folds, use pipelines, and avoid target leakage.
  • Cross-validation: choose between k-fold, stratified, group, or time-series split based on data structure.
  • Residual diagnostics: check for non-linearity, heteroscedasticity, outliers, and autocorrelation.
  • Nested cross-validation for hyperparameter tuning to avoid overfitting the validation set.
  • Appropriate evaluation metrics and statistical significance of performance differences.
  • Production monitoring: track residuals, feature drift, and model performance over time.

AI-generated suggestions, not part of the candidate's original notes. May be inaccurate — verify before relying on them.