Forward-fill None
Python data engineering interview problem. Difficulty: beginner. Pattern: Arrays. About 8 minutes. Free to practice.
Replace each None with the closest earlier value; leading Nones stay None. Treat this as a production helper: match the contracted return shape, including empty and duplicate inputs.
A time series often omits a value when a sensor has not changed. Fill only from the latest real value already seen. Return a new list replacing each None with the most recent earlier non-None value. Leading None values stay None, and falsey values are still real values. Write ffill(values: list) -> list. [0, None, 5, None] becomes [0, 0, 5, 5]. The starter file already contains the function name, harness and print lines. Keep them and fill only the function body.
Requirements
- Keep leading None values.
- Use the latest earlier real value.
- Do not mutate the input list.
Constraints
- None is the missing marker; 0, False and empty strings are valid values.
- Return a new list with the same length.
Examples
Input: ffill([0, None, 5, None]) Output: [0, 0, 5, 5] 0 is a real value, so the None after it becomes 0; the last None takes 5.
Topics: lakebench, python, fill, none, list.
More Python interview questions · All interview problems · Learn data engineering
Forward-fill None
Interview-style drill: Replace each None with the closest earlier value; leading Nones stay None.
Implement `ffill(values: list) -> list` that returns a new list where every `None` is replaced by the most recent earlier value that is not `None`. Nones before the first real value stay `None`. Real values such as `0`, `False` and `""` are values and must be kept and used for filling. The input list must not change.