Skip to main content

Fill Missing Data - Solution & Explanation

Easy12 min readAsked at: Microsoft, Google, Acko
Practice this problem

Problem Statement

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.

Approach Overview

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 1: Iterate and Replace Using Conditional Logic

This method involves iterating over each row of the DataFrame. We check if the 'quantity' value is null (or equivalent) and set it to zero if it is. This is a straightforward approach that uses conditional checks within loops appropriate for each programming language.

In this C code, we define an array of structs, each representing a product. The quantity is assumed to be -1 when missing. The function fillMissingQuantities iterates over the products and sets any -1 quantity to 0. Finally, we print out the updated products.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), as we do not use additional space proportional to the input size.

Try this approach in the editor →

Approach 2: Use Built-In Functions or Libraries

This approach involves leveraging built-in functions or libraries available in programming languages, like pandas in Python or LINQ in C#, to perform the task of filling in missing values effectively and efficiently.

Here, we stick with the manual way as C does not have a specific library for DataFrame handling. We assume -1 indicates a missing value, replaced by 0.

Code

C

C++

Java

Python

C#

JavaScript

Complexity

Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), without additional space for the task.

Try this approach in the editor →

Approach 3: Default Approach

Code

Python

Try this approach in the editor →

Complexity Comparison

ApproachComplexity
Iterate and Replace Using Conditional Logic

Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), as we do not use additional space proportional to the input size.

Use Built-In Functions or Libraries

Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), without additional space for the task.

Default Approach—

Detailed Complexity Analysis

ApproachTimeSpaceWhen to Use
Iterate and Replace Using Conditional LogicO(n)O(1)Best for interviews and when you want full control over how missing values are handled
Use Built-In Functions or LibrariesO(n)O(1) to O(n)Useful in production environments where standard library utilities simplify data cleaning

Video Solution

Fill Missing Data LeetCode 2887 • CuteLeetCrafter • 249 views views

Watch 3 more video solutions →

Frequently Asked Questions

Is Fill Missing Data easy or hard?
Fill Missing Data is generally classified as an easy problem. The solution relies on straightforward array traversal and conditional updates rather than advanced algorithms or data structures.
Fill Missing Data Python/Java solution
In Python, iterate through the list and replace missing values using conditional checks or built-in utilities. In Java, loop through the array and update elements directly while maintaining the last valid value. Both implementations run in O(n) time and require constant extra space.
How to solve Fill Missing Data in O(n)?
Perform a single linear scan through the array. For each element, check if it represents missing data. If it does, replace it using the defined rule (such as the last valid value encountered). Maintaining a variable that stores the most recent valid entry allows the entire operation to complete in O(n) time.
What is the best approach for Fill Missing Data?
The most reliable approach is iterating through the sequence and replacing missing entries using conditional logic. Track the last valid value or compute the replacement according to the problem rule while scanning the array. This method runs in O(n) time and uses O(1) extra space when updates are done in place.
Is Fill Missing Data asked at Google/Amazon/Meta?
Problems involving filling or propagating missing values appear in interviews at major tech companies, especially in data-processing or backend roles. Variations test your ability to iterate efficiently, maintain state during traversal, and handle edge cases in arrays or datasets.
What data structure is used in Fill Missing Data?
The problem primarily uses arrays or lists because the data must be scanned sequentially. A small number of variables are used to track the most recent valid value or replacement rule, so no complex data structures are required.
What is the time complexity of Fill Missing Data?
The standard solution processes each element exactly once, resulting in O(n) time complexity where n is the number of data points. Space complexity is typically O(1) when modifying the array directly, though some library-based implementations may use O(n) if they return a new structure.

Ready to solve this problem?

Practice Fill Missing Data with our built-in code editor and test cases.

Practice on FleetCode