Watch 5 video solutions for Fill Missing Data, a easy level problem. This walkthrough by CuteLeetCrafter has 245 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
DataFrame products
+-------------+--------+
| Column Name | Type |
+-------------+--------+
| name | object |
| quantity | int |
| price | int |
+-------------+--------+
Write a solution to fill in the missing value as 0 in the quantity column.
The result format is in the following example.
Example 1: Input:+-----------------+----------+-------+ | name | quantity | price | +-----------------+----------+-------+ | Wristwatch | None | 135 | | WirelessEarbuds | None | 821 | | GolfClubs | 779 | 9319 | | Printer | 849 | 3051 | +-----------------+----------+-------+ Output: +-----------------+----------+-------+ | name | quantity | price | +-----------------+----------+-------+ | Wristwatch | 0 | 135 | | WirelessEarbuds | 0 | 821 | | GolfClubs | 779 | 9319 | | Printer | 849 | 3051 | +-----------------+----------+-------+ Explanation: The quantity for Wristwatch and WirelessEarbuds are filled by 0.
Problem Overview: You receive a sequence containing missing or placeholder values. The task is to scan the data and replace those missing entries with valid values based on a defined rule, typically derived from surrounding elements or previously seen data.
Approach 1: Iterate and Replace Using Conditional Logic (Time: O(n), Space: O(1))
The direct solution is a single pass through the sequence. While iterating, check each element for a missing marker such as null, None, or a sentinel value. When a missing entry appears, replace it using the rule defined by the problem (commonly the last valid value seen or another computed fallback). This approach relies on simple array traversal and conditional checks, so the logic stays predictable and efficient.
Because the array is processed once and updates are done in place, the time complexity is O(n) and the space complexity is O(1). This method is reliable for interview settings since it demonstrates strong control over iteration and state tracking.
Approach 2: Use Built-In Functions or Libraries (Time: O(n), Space: O(1) to O(n))
Many languages provide utilities that simplify missing-value handling. For example, libraries may offer forward-fill or replace operations that automatically propagate valid values into missing slots. Internally these functions still perform a sequential scan, but they hide the explicit loop and conditional checks from the user.
This approach keeps the time complexity at O(n) because the dataset must still be processed element by element. Space usage depends on the implementation: in-place replacements use O(1) space, while functions that return a new structure may require O(n). It works well in production code where readability and reliability matter more than manually implementing iteration.
Recommended for interviews: Start with the explicit iteration approach. Interviewers expect you to control the scan, track the last valid value, and update missing entries as you go. The built-in function approach is useful in real-world data processing, but writing the logic yourself demonstrates stronger understanding of array processing and sequential iteration.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Iterate and Replace Using Conditional Logic | O(n) | O(1) | Best for interviews and when you want full control over how missing values are handled |
| Use Built-In Functions or Libraries | O(n) | O(1) to O(n) | Useful in production environments where standard library utilities simplify data cleaning |