← Pinterest Interview Insights

Pinterest·Machine Learning Engineer·Technical Phone Screen·Intermediate

Intermediate
Apr 2026

Summary

Pinterest ML Engineer interview had a coding round focused on implementing a bagging classifier from scratch, no NumPy allowed. Pretty implementation-heavy, less theory than I expected.

Questions Asked (3)

Q1

Implement a bootstrapping function that samples n training examples with replacement from a dataset, returning the sampled features and labels.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Straightforward once you realize it's just random.choices with k=len(X).

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the input format (e.g., NumPy arrays or tensors) and the expected output. Then, implement the bootstrapping by generating n random indices with replacement and using them to index the features and labels. Discuss the importance of setting a random seed for reproducibility and consider vectorization for efficiency.

Pro tip: Mention that bootstrapping is often used in ensemble methods like Random Forests and that returning the indices can be useful for out-of-bag evaluation. Also, highlight the need to handle large datasets efficiently by using vectorized operations.

1. Clarify requirements

Ask about the input data types (e.g., NumPy arrays, PyTorch tensors) and whether the function should return indices or just the sampled data. Confirm if reproducibility is needed (random seed).

2. Generate random indices

Use a random number generator to create n indices in the range [0, len(dataset)-1] with replacement. Ensure the indices are integers.

3. Index the dataset

Use the generated indices to select the corresponding features and labels from the dataset. If using NumPy, this can be done via array indexing; if using PyTorch, use torch.index_select or advanced indexing.

4. Return sampled data

Return the sampled features and labels as separate arrays/tensors. Optionally, also return the indices for further analysis (e.g., out-of-bag error).

5. Discuss trade-offs and optimizations

Mention potential optimizations like vectorization, memory considerations for large n, and the impact of sampling with replacement on data distribution.

Key Points to Mention

  • Sampling with replacement means some examples may be repeated and others omitted.
  • Use of random seed for reproducibility in experiments.
  • Vectorized operations (e.g., NumPy's np.random.choice or torch.randint) for efficiency.
  • Returning indices can be useful for out-of-bag evaluation in ensemble methods.
  • Memory and computational trade-offs when n is large.
  • Bootstrapping is a key technique in bagging and Random Forests.

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

Q2

Implement the fit method for a BaggingClassifier that trains multiple decision trees, each on a different bootstrap sample of the training data.

Algorithms & Data StructuresSystem Design
Author's notes

This part was fine.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the interface and assumptions (e.g., base estimator, number of estimators, bootstrap sampling). Then outline the fit method: initialize storage for models, loop to create bootstrap samples, train each estimator on its sample, and store the trained models. Finally, discuss optional features like out-of-bag score and parallelism.

Pro tip: Mention that you would use vectorized sampling with numpy for efficiency and consider parallel training with joblib to leverage multiple cores, which is crucial for large datasets at Pinterest scale.

1. Clarify requirements and interface

Confirm the expected input/output, parameters (n_estimators, max_samples, bootstrap), and whether to support out-of-bag evaluation. This ensures alignment with the interviewer's expectations.

2. Initialize data structures

Create a list to store the trained estimators and optionally an array to store out-of-bag indices or scores. Pre-allocate for efficiency.

3. Generate bootstrap samples

For each estimator, randomly sample indices with replacement from the training data. Use numpy's random choice for vectorized sampling.

4. Train and store estimators

Instantiate a new base estimator (e.g., DecisionTreeClassifier), fit it on the bootstrap sample, and append it to the list of models. Optionally compute out-of-bag score.

5. Finalize and return self

Store any additional attributes (e.g., n_features, classes_) and return self to allow method chaining, following scikit-learn conventions.

Key Points to Mention

  • Bootstrap sampling: sampling with replacement, typically same size as original dataset.
  • Parallelization: using joblib to train trees in parallel for scalability.
  • Out-of-bag evaluation: using samples not in bootstrap for validation.
  • Random seed handling: ensuring reproducibility by setting random_state.
  • Memory efficiency: avoiding unnecessary data copies, using indices instead of data copies.
  • Integration with scikit-learn API: fit returns self, attributes end with underscore.

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

Q3

Implement the predict method using majority vote aggregation across all trained trees, and explain how you handle tie-breaking.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Majority vote using a counter per row is simple enough, but they pushed on tie-breaking and I hadn't thought about it at all.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by outlining the predict method's logic: iterate over all trees, collect their predictions, and aggregate via majority vote. Then discuss tie-breaking strategies, such as random selection or using class priors, and justify your choice based on the application context.

Pro tip: Mention that tie-breaking should be deterministic for reproducibility, and consider using the class distribution from training data as a fallback. This shows you think about production reliability and edge cases.

1. Clarify the problem and assumptions

Confirm that the forest consists of classification trees and that each tree outputs a class label. Assume binary or multiclass classification and that all trees are trained.

2. Outline the majority vote aggregation

Explain that for each input sample, you collect predictions from all trees, count the votes per class, and select the class with the highest count.

3. Address tie-breaking explicitly

Describe how to handle ties: e.g., choose the class with the highest prior probability, or randomly select among tied classes. Emphasize deterministic tie-breaking for reproducibility.

4. Discuss implementation details

Mention using a dictionary or array to tally votes, and iterating over trees efficiently. Consider vectorization or parallelization for performance.

5. Evaluate trade-offs and edge cases

Talk about the impact of tie-breaking on model performance, and how to handle cases with no trees or empty predictions. Suggest logging ties for monitoring.

Key Points to Mention

  • Majority vote aggregation: count predictions per class and select the mode.
  • Tie-breaking strategies: random selection, class priors, or first class encountered.
  • Deterministic tie-breaking for reproducibility and production stability.
  • Efficiency considerations: time complexity O(n_trees * n_samples).
  • Edge cases: no trees, empty predictions, or all trees predicting different classes.
  • Potential use of weighted voting if trees have varying confidence or performance.

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