




Sponsored
Sponsored
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.
class Product {
    public string Name { get; set; }
    public int? Quantity { get; set; }
    public int Price { get; set; }
}
class FillMissingData {
    static void FillMissingQuantities(Product[] products) {
        foreach (var product in products) {
            if (!product.Quantity.HasValue) {
                product.Quantity = 0;
            }
        }
    }
    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.
1let products = [
2    { name: "Wristwatch", quantity: null, price: 135 },
3    { name: "WirelessEarbuds", quantity: null, price: 821 },
4    { name: "GolfClubs", quantity: 779, price: 9319 },
5    { name: "Printer", quantity: 849, price: 3051 },
6];
7
8products = products.map(product => ({
9    ...product,
10    quantity: product.quantity ?? 0
11}));
12
13console.log(products);JavaScript's map function creates a new array based on the previous one, updating quantity fields with nullish coalescing to handle null values effectively.