← Lyft Interview Insights

Lyft·Data Scientist·Technical Phone Screen·Intermediate

Intermediate
Jun 2026

Summary

Second round coding interview at Lyft for a data scientist role. Just one problem but it was a real one.

Questions Asked (1)

Q1

Implement stratified k-fold cross validation from scratch.

Algorithms & Data StructuresTechnical Trade-offs
Author's notes

Not the kind of thing you'd expect to code live.

Create a free account to read the full note

AI HintsAI Generated

Suggested Approach

Start by clarifying the requirements: stratified k-fold ensures each fold preserves the class distribution of the full dataset. Then outline the algorithm: for each class, split its indices into k folds, then combine corresponding folds across classes to form the final k folds. Finally, discuss implementation details like handling class imbalance and edge cases.

Pro tip: Mention that you would use this for imbalanced datasets and that you'd verify the stratification by checking class proportions in each fold. Also, note that you can leverage numpy for efficient index manipulation.

1. Clarify requirements and assumptions

Confirm that the goal is to maintain the same class distribution in each fold as in the full dataset. Ask about input format (e.g., numpy arrays, pandas DataFrame) and whether shuffling is needed.

2. Group indices by class

For each class label, collect the indices of samples belonging to that class. This can be done using a dictionary or list of lists.

3. Split each class into k folds

For each class, shuffle its indices (if desired) and split them into k approximately equal-sized folds. Use numpy.array_split for convenience.

4. Combine folds across classes

For each fold i from 0 to k-1, concatenate the i-th fold from each class to form the final fold i. This ensures each final fold has the same class proportions.

5. Generate train/test splits and validate

For each fold, use it as the test set and the remaining folds as the training set. Optionally, verify that class distributions are preserved.

Key Points to Mention

  • Stratification is crucial for imbalanced datasets to ensure each fold is representative.
  • Implementation can be done by splitting each class separately and then combining.
  • Use numpy for efficient index manipulation and shuffling.
  • Handle edge cases: classes with fewer samples than k folds (e.g., use k-1 or warn).
  • Shuffling within each class before splitting helps avoid ordering bias.
  • Validation: check that class proportions in each fold match the overall distribution.

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