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.
The key idea in #2887 Fill Missing Data is to scan the dataset and replace undefined or missing entries using available information from nearby or previously processed values. A common strategy is to iterate through the array or table and track the most recent valid value. When a missing element is encountered, it can often be filled using the last known value, the next valid value, or a simple rule derived from neighboring elements.
In many implementations, a single pass traversal is sufficient. During traversal, maintain variables that store the latest valid data or compute replacements based on surrounding context. In some variations, a forward pass followed by a backward pass ensures all missing entries receive appropriate values.
This approach works efficiently because each element is processed only once or twice, avoiding unnecessary nested operations. As a result, the algorithm typically runs in linear time while requiring minimal additional memory.
| Approach | Time Complexity | Space Complexity |
|---|---|---|
| Single-pass traversal with tracking of last/next valid value | O(n) | O(1) |
| Forward and backward pass filling strategy | O(n) | O(1) |
NeetCode
Use these hints if you're stuck. Try solving on your own first.
Consider using a build-in function in pandas library to fill the missing values of specified columns.
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.
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.
1using System;
2
3class Product {
4 public string Name { get; set; }
5 public int? Quantity { get; set; }
6 public int Price { get; set; }
7}
8
9class FillMissingData {
10 static void FillMissingQuantities(Product[] products) {
11 foreach (var product in products) {
12 if (!product.Quantity.HasValue) {
13 product.Quantity = 0;
14 }
}
}
static void Main() {
Product[] products = {
new Product { Name = "Wristwatch", Quantity = null, Price = 135 },
new Product { Name = "WirelessEarbuds", Quantity = null, Price = 821 },
new Product { Name = "GolfClubs", Quantity = 779, Price = 9319 },
new Product { Name = "Printer", Quantity = 849, Price = 3051 }
};
FillMissingQuantities(products);
foreach (var product in products) {
Console.WriteLine($"{product.Name}: {product.Quantity} {product.Price}");
}
}
}C# uses nullable types for quantities to represent missing values. The FillMissingQuantities method checks these values and assigns 0 where necessary. The program outputs the adjusted data to the console.
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.
Time Complexity: O(n), where n is the number of products.
Space Complexity: O(1), without additional space for the task.
1using System;
using System.Linq;
class Product {
public string Name { get; set; }
public int? Quantity { get; set; }
public int Price { get; set; }
}
class FillMissingData {
static void FillMissingQuantities(Product[] products) {
products = products.Select(p =>
{
if (!p.Quantity.HasValue)
p.Quantity = 0;
return p;
}).ToArray();
}
static void Main() {
Product[] products = {
new Product { Name = "Wristwatch", Quantity = null, Price = 135 },
new Product { Name = "WirelessEarbuds", Quantity = null, Price = 821 },
new Product { Name = "GolfClubs", Quantity = 779, Price = 9319 },
new Product { Name = "Printer", Quantity = 849, Price = 3051 }
};
FillMissingQuantities(products);
foreach (var product in products) {
Console.WriteLine($"{product.Name}: {product.Quantity} {product.Price}");
}
}
}Watch expert explanations and walkthroughs
Jot down your thoughts, approach, and key learnings
Yes, the problem is typically solved in O(n) time by scanning the dataset once or twice. Each element is processed a constant number of times, making the approach efficient for large inputs.
Yes, variations of missing-data or gap-filling problems appear in coding interviews, especially when testing array traversal and data preprocessing skills. Interviewers often focus on clean iteration and efficient handling of edge cases.
The optimal approach is usually a linear traversal where you track previously seen valid values and use them to fill missing entries. Some variations also use a forward and backward pass to ensure every missing element gets the most appropriate value.
Most solutions rely on simple arrays or lists since the problem typically involves sequential traversal. A few helper variables are enough to store the last or next valid value while filling the missing entries.
C#'s LINQ framework facilitates processing transformations with the Select method, allowing streamlined handling of nullable types in objects.