KatInterview

Review a Fit/Transform Preprocessing Module and Write a Key Test

Data Structure DesignModel answer
MediumModel answerData Structure Designpythoncode-reviewscikit-learn

Last updated

Review a small Python preprocessing module with an outlier-capping class and three imputers: explain its purpose, justify fit/transform separation, critique style and imports, and write one key unit test.

Scenario

You are reviewing a pull request for a small Python data-preprocessing module. It contains:

  • an OutlierHandler class that treats extreme numeric values, and
  • three imputer classes that fill in missing values.

You do not have to run the code. Assume it follows the typical shape of such modules: OutlierHandler learns per-column limits from data and then applies them, and the imputers each use a different statistic (for example mean, median, or most frequent value) to fill gaps.

What to cover

  1. Purpose. Give a high-level summary of what OutlierHandler accomplishes and how it does so.
  2. Design. Explain why it is beneficial for the class to expose learning (fit) and applying (transform) as two separate methods.
  3. Style review. Identify the coding-style and import problems you would expect in the three imputer classes. For each, say why it matters and how you would fix it.
  4. Testing. Write, by hand, one unit test for OutlierHandler that you consider the most valuable. Explain what bug it would catch and use a clear expected-versus-actual comparison.

Expectations

  • Reference conventions from scikit-learn estimators and PEP 8.
  • Be concrete: name specific problems and specific fixes rather than generalities.
  • The test should be small, deterministic, and include hand-computed expected values.

Hints

Hint 1

Think about what would go wrong if the limits were recomputed from whatever data is passed to the applying step, for example when scoring a single row in production or evaluating on a test set.

Hint 2

For the style review, look at how names are imported (wildcard imports), whether the three imputers duplicate logic, naming conventions, whether fit returns self, and whether the input data is modified.

Hint 3

For the test, fit on a tiny dataset where you can compute the quartiles by hand, transform a different dataset containing values far outside the range, and assert that results are capped at the training-derived limits and that the input was not changed.