Articles
Machine Learning Coding Interview Questions: NumPy, Pandas, and Model Implementation
Practice machine learning coding interview questions across NumPy, pandas, and model implementation with a timed workflow and logistic regression example.
Your solution returns the right metric on the sample data. Then the interviewer asks three follow-ups: What if the join key is duplicated? Did you fit the imputer before or after the split? What happens when a logit is 900?
That is the real machine learning coding interview. The task may look like a short NumPy or pandas exercise, but the signal comes from whether you can turn messy data into a correct, numerically stable, testable modeling pipeline.
Then browse Machine Learning Engineer interview questions and company-tagged Machine Learning questions to practice the exact mix your target role uses.
Strong ML coding answers connect array operations, data contracts, model math, and observable tests.
Quick Verdict
Prepare for an auditable pipeline, not a collection of memorized APIs. You should be able to inspect shapes, manipulate tables, implement a simple model from first principles, choose a metric, test edge cases, and explain where the solution would break at production scale.
A strong candidate can trace one example from raw row to feature vector to prediction to metric without hiding behind a library.
What Is a Machine Learning Coding Interview?
A machine learning coding interview is a hands-on round that tests whether you can express ML reasoning in working code. Some rounds isolate one skill, such as vectorizing a NumPy function. Others combine data preparation, model implementation, evaluation, and follow-up discussion in one notebook or live editor.
The exact format varies by company and team.
| Lane | Typical deliverable | What the interviewer learns |
|---|---|---|
| NumPy | Vectorized tensor or matrix function | Shape reasoning, numerical stability, memory awareness |
| pandas | Correct feature table from messy inputs | Join semantics, grouping, missing data, leakage control |
| Model implementation | Training and prediction without a high-level estimator | Understanding of objectives, gradients, and convergence |
| Evaluation | Metric calculation and error analysis | Whether model quality matches the decision being made |
| Extension | Tests, complexity, and production trade-offs | Engineering judgment beyond the happy path |
What Interviewers Score Beyond Correct Output
| Signal | Strong evidence | Weak evidence |
|---|---|---|
| Correctness | States shapes, invariants, and expected output before coding | Relies on the sample output alone |
| Data hygiene | Checks keys, dtypes, missing values, and split boundaries | Silently accepts row multiplication or leakage |
| Numerical stability | Handles large logits, zero variance, and safe denominators | Produces inf or NaN on valid edge cases |
| Efficiency | Uses vectorized operations while tracking intermediate memory | Uses row loops or creates an enormous broadcasted tensor |
| Model reasoning | Connects the objective, update rule, and metric | Recites code without explaining why it learns |
| Communication | Narrates assumptions, tests, and deliberate scope choices | Codes silently until time expires |
NumPy's official broadcasting guide makes the central trade-off explicit: broadcasting can move loops into efficient compiled operations, but a large intermediate array can still waste memory. Interviewers care whether you see both sides.
Study the three lanes together because follow-ups often cross their boundaries.
A 50-Minute ML Coding Interview Plan
| Time | Action | Visible output |
|---|---|---|
| 0-5 min | Clarify target, data shape, allowed libraries, split, and metric | Short problem contract |
| 5-10 min | Inspect keys, dtypes, missingness, class balance, and dimensions | Data and shape assertions |
| 10-25 min | Build the simplest correct vectorized data path | Clean train and validation matrices |
| 25-38 min | Implement training, prediction, and a baseline metric | Runnable end-to-end model |
| 38-46 min | Test edge cases and inspect convergence | Evidence the implementation works |
| 46-50 min | Explain complexity, limitations, and the next improvement | Prioritized follow-ups |
This is a practice framework, not a universal interview schedule. If the prompt is only one NumPy function, spend the saved setup time on shape examples, boundary tests, and complexity.
Keep one loop visible: clarify, inspect, transform, implement, test, and explain.
Machine Learning Coding Interview Questions to Practice
NumPy Questions
| Prompt | Likely follow-up |
|---|---|
| Implement stable softmax for a 2D batch | Why subtract the row maximum, and which axis is correct? |
| Standardize each feature column | What should happen when a column has zero variance? |
| Compute pairwise Euclidean distances | Can you avoid a huge n x m x d intermediate? |
| Return top-k cosine-similar rows | How do you handle zero-norm vectors and ties? |
| Build a confusion matrix without loops | How do labels map to bins, and what if a class is absent? |
pandas Questions
| Prompt | Likely follow-up |
|---|---|
| Join users to aggregated events | How do you prove the join did not multiply rows? |
| Keep the latest record per entity | How do timestamps, ties, and null IDs behave? |
| Create rolling 7-day features | Did any event after the prediction time enter the feature? |
| Fill missing values and encode categories | Were statistics and vocabulary fitted on training data only? |
| Return the top three rows per group | What is the deterministic tie-breaking rule? |
Use explicit merge contracts. The pandas documentation notes that duplicate keys can create a Cartesian product, and its validate argument can check expected key uniqueness before the merge completes.
Model Implementation Questions
| Prompt | What to explain |
|---|---|
| Linear regression with gradient descent | Gradient shape, learning rate, scaling, and MSE |
| Binary logistic regression from scratch | Sigmoid, log loss, threshold, and regularization |
| K-means clustering | Initialization, convergence, and empty clusters |
| Multinomial Naive Bayes | Log probabilities, smoothing, and unseen tokens |
| One dense neural layer | Forward shapes, activation derivative, and gradient checks |
Worked Example: Logistic Regression with pandas and NumPy
Suppose you receive user and event tables and must predict a binary outcome. High-level estimators are not allowed. The goal is not to recreate every production feature; it is to make the smallest leakage-safe pipeline work and defend every choice.
1. Build a Data Contract Before a Model
Clarify the prediction timestamp, label horizon, positive class, allowed features, split strategy, and primary metric. Ask whether categorical encoding is required and whether the interviewer expects a closed-form solution or gradient descent.
For time-dependent data, decide the cutoff before calculating imputations or scaling statistics. A random split can leak future behavior even when the code itself looks clean.
2. Create Features and Fit Preprocessing on Training Data
feature_rows = (
events.groupby("user_id", as_index=False)
.agg(sessions_7d=("session_id", "nunique"),
spend_30d=("amount", "sum"))
)
df = users.merge(
feature_rows,
on="user_id",
how="left",
validate="one_to_one",
)
features = ["age", "sessions_7d", "spend_30d"]
train = df.loc[df["as_of"] < "2026-01-01"].copy()
valid = df.loc[df["as_of"] >= "2026-01-01"].copy()
medians = train[features].median()
X_train = train[features].fillna(medians).to_numpy(dtype=float)
X_valid = valid[features].fillna(medians).to_numpy(dtype=float)
y_train = train["label"].to_numpy(dtype=float)
y_valid = valid["label"].to_numpy(dtype=float)
mean = X_train.mean(axis=0)
scale = X_train.std(axis=0)
scale = np.where(scale < 1e-12, 1.0, scale)
X_train = (X_train - mean) / scale
X_valid = (X_valid - mean) / scale
The key detail is not the syntax. Every fitted quantity comes from the training partition. The validation set reuses the training medians, means, and scales. For categorical features, fit the vocabulary on training data and define how unseen values map at validation or inference time.
3. Implement a Numerically Safer Training Loop
Google's logistic regression material describes the essential pieces: sigmoid maps the linear score to a probability, log loss measures the prediction, and regularization can reduce overfitting. In code, protect those equations from extreme values.
def sigmoid(z):
z = np.clip(z, -30.0, 30.0)
return 1.0 / (1.0 + np.exp(-z))
def fit_logistic(X, y, lr=0.1, steps=1000, l2=0.0):
n, d = X.shape
w = np.zeros(d)
b = 0.0
losses = []
for step in range(steps):
p = sigmoid(X @ w + b)
error = p - y
grad_w = (X.T @ error) / n + (l2 / n) * w
grad_b = error.mean()
w -= lr * grad_w
b -= lr * grad_b
if step % 100 == 0:
safe_p = np.clip(p, 1e-12, 1.0 - 1e-12)
loss = -np.mean(
y * np.log(safe_p) + (1 - y) * np.log(1 - safe_p)
) + (l2 / (2 * n)) * (w @ w)
losses.append(loss)
return w, b, losses
def predict_proba(X, w, b):
return sigmoid(X @ w + b)
Narrate the dimensions: X is (n, d), w is (d,), and X @ w is (n,). Explain why the bias is not regularized here, how the learning rate affects convergence, and why a fixed 0.5 threshold may not match an imbalanced business decision.
4. Test the Contract, Not Just One Accuracy Number
w, b, losses = fit_logistic(X_train, y_train)
p_valid = predict_proba(X_valid, w, b)
assert w.shape == (X_train.shape[1],)
assert p_valid.shape == (len(X_valid),)
assert np.isfinite(p_valid).all()
assert ((0.0 <= p_valid) & (p_valid <= 1.0)).all()
assert losses[-1] <= losses[0]
Then evaluate the metric requested by the prompt and inspect at least one failure mode. A decreasing training loss is useful, but it does not prove generalization, prevent leakage, or guarantee that the chosen threshold serves the product objective.
Common Failure Modes and Better Fixes
| Failure | Why it matters | Better move |
|---|---|---|
| Using apply(axis=1) immediately | Hides a vectorizable operation in Python-level work | Try masks, groupby, transform, or broadcasting first |
| Fitting preprocessing on all rows | Leaks validation information into training | Split first; fit medians, scales, and vocabularies on train only |
| Ignoring duplicate join keys | Changes row counts and corrupts labels or features | Assert uniqueness and use validate |
| Writing unstable math | Extreme values produce overflow or undefined logs | Shift, clip, or use stable identities and test extremes |
| Optimizing before a baseline works | Consumes time without producing an end-to-end result | Finish one correct path, then improve the bottleneck |
| Reporting accuracy only | May hide poor minority-class behavior | Connect the metric and threshold to the decision cost |
A 7-Day Practice Plan
| Day | Focus | Timed artifact |
|---|---|---|
| 1 | NumPy shapes, axes, masks, and broadcasting | Stable softmax plus five edge tests |
| 2 | pandas joins, grouping, and deduplication | One feature table with row-count assertions |
| 3 | Missing data, encoding, and leakage-safe splitting | Reusable preprocessing function |
| 4 | Linear and logistic regression | Training loop plus convergence plot or log |
| 5 | K-means or Naive Bayes | From-scratch implementation with edge cases |
| 6 | Metrics and imbalanced thresholds | Confusion matrix, precision, recall, and F1 |
| 7 | Full timed mock | One unfamiliar company-tagged prompt and postmortem |
Machine Learning Coding Interview FAQ
Do I need to memorize every NumPy and pandas method?
No. Memorize a compact working set: shapes and axes, boolean masks, reductions, broadcasting, joins, grouping, sorting, missing-value operations, and conversion to arrays. More importantly, practice recognizing data contracts and checking results. If documentation is allowed, knowing what to verify matters more than recalling every argument.
Can I use scikit-learn in an ML coding interview?
Only if the prompt allows it. Ask before coding. Some rounds test applied library usage; others intentionally prohibit high-level estimators so you must implement the objective and update rule. Even when scikit-learn is allowed, be prepared to explain preprocessing fit, leakage, metric choice, and model behavior.
Will an MLE interview still include normal DSA coding?
It can. A machine learning coding round does not necessarily replace the general algorithms round. Confirm the loop with your recruiter, then prepare each format separately: DSA for algorithmic reasoning, ML coding for data and model implementation, and ML system design for production architecture.
Which models should I be able to implement from scratch?
Start with linear regression, logistic regression, k-means, and Naive Bayes. They cover gradients, matrix operations, convergence, probability, and smoothing without excessive code. Add a small neural layer or decision stump when the role emphasizes deep learning or tree methods.
Final Takeaway
Machine learning coding interviews reward visible reasoning. Clarify the data contract, write shape-aware operations, protect the split from leakage, implement one defensible baseline, test numerical and data edge cases, and connect the metric to the decision.
Repeating that full loop is more useful than reading another list of disconnected API questions.