cswork / libraryOpen practice workspace

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.

LaneTypical deliverableWhat the interviewer learns
NumPyVectorized tensor or matrix functionShape reasoning, numerical stability, memory awareness
pandasCorrect feature table from messy inputsJoin semantics, grouping, missing data, leakage control
Model implementationTraining and prediction without a high-level estimatorUnderstanding of objectives, gradients, and convergence
EvaluationMetric calculation and error analysisWhether model quality matches the decision being made
ExtensionTests, complexity, and production trade-offsEngineering judgment beyond the happy path

What Interviewers Score Beyond Correct Output

SignalStrong evidenceWeak evidence
CorrectnessStates shapes, invariants, and expected output before codingRelies on the sample output alone
Data hygieneChecks keys, dtypes, missing values, and split boundariesSilently accepts row multiplication or leakage
Numerical stabilityHandles large logits, zero variance, and safe denominatorsProduces inf or NaN on valid edge cases
EfficiencyUses vectorized operations while tracking intermediate memoryUses row loops or creates an enormous broadcasted tensor
Model reasoningConnects the objective, update rule, and metricRecites code without explaining why it learns
CommunicationNarrates assumptions, tests, and deliberate scope choicesCodes 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

TimeActionVisible output
0-5 minClarify target, data shape, allowed libraries, split, and metricShort problem contract
5-10 minInspect keys, dtypes, missingness, class balance, and dimensionsData and shape assertions
10-25 minBuild the simplest correct vectorized data pathClean train and validation matrices
25-38 minImplement training, prediction, and a baseline metricRunnable end-to-end model
38-46 minTest edge cases and inspect convergenceEvidence the implementation works
46-50 minExplain complexity, limitations, and the next improvementPrioritized 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

PromptLikely follow-up
Implement stable softmax for a 2D batchWhy subtract the row maximum, and which axis is correct?
Standardize each feature columnWhat should happen when a column has zero variance?
Compute pairwise Euclidean distancesCan you avoid a huge n x m x d intermediate?
Return top-k cosine-similar rowsHow do you handle zero-norm vectors and ties?
Build a confusion matrix without loopsHow do labels map to bins, and what if a class is absent?

pandas Questions

PromptLikely follow-up
Join users to aggregated eventsHow do you prove the join did not multiply rows?
Keep the latest record per entityHow do timestamps, ties, and null IDs behave?
Create rolling 7-day featuresDid any event after the prediction time enter the feature?
Fill missing values and encode categoriesWere statistics and vocabulary fitted on training data only?
Return the top three rows per groupWhat 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

PromptWhat to explain
Linear regression with gradient descentGradient shape, learning rate, scaling, and MSE
Binary logistic regression from scratchSigmoid, log loss, threshold, and regularization
K-means clusteringInitialization, convergence, and empty clusters
Multinomial Naive BayesLog probabilities, smoothing, and unseen tokens
One dense neural layerForward 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

FailureWhy it mattersBetter move
Using apply(axis=1) immediatelyHides a vectorizable operation in Python-level workTry masks, groupby, transform, or broadcasting first
Fitting preprocessing on all rowsLeaks validation information into trainingSplit first; fit medians, scales, and vocabularies on train only
Ignoring duplicate join keysChanges row counts and corrupts labels or featuresAssert uniqueness and use validate
Writing unstable mathExtreme values produce overflow or undefined logsShift, clip, or use stable identities and test extremes
Optimizing before a baseline worksConsumes time without producing an end-to-end resultFinish one correct path, then improve the bottleneck
Reporting accuracy onlyMay hide poor minority-class behaviorConnect the metric and threshold to the decision cost

A 7-Day Practice Plan

DayFocusTimed artifact
1NumPy shapes, axes, masks, and broadcastingStable softmax plus five edge tests
2pandas joins, grouping, and deduplicationOne feature table with row-count assertions
3Missing data, encoding, and leakage-safe splittingReusable preprocessing function
4Linear and logistic regressionTraining loop plus convergence plot or log
5K-means or Naive BayesFrom-scratch implementation with edge cases
6Metrics and imbalanced thresholdsConfusion matrix, precision, recall, and F1
7Full timed mockOne 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.

Sources and Further Reading