Coding questions · Capital One
Critique and Test Python Preprocessing Utilities Effectively
This question evaluates a candidate's ability to review and critique Python preprocessing utilities, covering competencies in software design patterns (fit/transform separation), coding style and import hygiene, and unit-test specification for data-science pipelines.
Scenario
Code review of Python preprocessing utilities (OutlierHandler and three Imputer classes)
Question
Summarize, at a high level, what the OutlierHandler class accomplishes. Why is keeping fit and transform as separate methods advantageous? Identify and critique any coding-style or import problems found in the three Imputer classes. Hand-write one critical unit test you would add for OutlierHandler.
Hints
Think scikit-learn patterns, PEP-8, avoiding "from numpy import *", assert expected vs. actual.
Implement a Fit/Transform Outlier Handler
In many preprocessing pipelines, a utility first learns statistics from training data (fit) and later applies them to any dataset (transform). Implement a simplified OutlierHandler.
Your function receives two lists:
train: the data used to learn outlier thresholdsvalues: the data to transform
Rules:
- Ignore
Nonevalues intrain. - Sort the remaining values and compute quartiles using this definition:
- If the number of values is odd, exclude the overall median when splitting into lower and upper halves.
Q1is the median of the lower half.Q3is the median of the upper half.- The median of an even-length list is the average of the two middle values.
- Compute
IQR = Q3 - Q1. - Compute bounds:
lower = Q1 - 1.5 * IQRupper = Q3 + 1.5 * IQR
- Transform each element in
values:- keep
NoneasNone - if a number is below
lower, replace it withlower - if a number is above
upper, replace it withupper - otherwise leave it unchanged
- keep
If train contains fewer than 2 non-None values, return values unchanged.
Return the transformed list.
Constraints
- 0 <= len(train), len(values) <= 100000
- Each element is either an integer, a float, or None
- Quartiles must be computed from
trainonly, never fromvalues
Examples
Example 1
Input: ([10, 12, 13, 14, 15, 16, 18, 19, 100], [5, 15, 30])
Output: [5, 15, 27.5]
Explanation: From the training data, Q1 = 12.5 and Q3 = 18.5, so IQR = 6. The bounds are 3.5 and 27.5. Only 30 is above the upper bound, so it is clipped to 27.5.
Example 2
Input: ([None, 1, 2, 2, 3, 4, 100], [None, -5, 8, 4])
Output: [None, -1.0, 7.0, 4]
Explanation: Ignoring None, the sorted training data is [1, 2, 2, 3, 4, 100]. Q1 = 2, Q3 = 4, so IQR = 2 and the bounds are -1 and 7. None stays None, -5 becomes -1, 8 becomes 7, and 4 stays unchanged.
Hints
- Write a small helper function to compute the median of a sorted list, then reuse it for Q1 and Q3.
- This is a fit/transform pattern: learn the clipping bounds from
trainonce, then apply them to every element invalues.