We use cookies (including Google cookies) to personalize ads and analyze traffic. By continuing to use our site, you accept our Privacy Policy.

Apply Discount Every n Orders

Number: 1459

Difficulty: Medium

Paid? No

Companies: Meta


Problem Description

Design a Cashier system for a supermarket. Given a list of products and their prices, the Cashier processes customer orders represented by lists of purchased product IDs and their corresponding amounts. Every nth customer receives a discount applied to the total bill. The discount is given as a percentage off the subtotal. Implement the Cashier class with a constructor to initialize the system parameters and a getBill method to compute the final bill with discounts applied when applicable.


Key Insights

  • Use a hash table (or dictionary) to map product IDs to their prices for constant time lookups.
  • Maintain a counter to track the number of customers processed.
  • Apply the discount to the bill when the customer's order count is a multiple of n.
  • The discount is computed by multiplying the subtotal with ((100 - discount) / 100).

Space and Time Complexity

Time Complexity: O(m) per getBill call, where m is the number of different products in the customer order. Space Complexity: O(p) where p is the number of products, needed to store the mapping of product IDs to prices.


Solution

We leverage a dictionary (hash table) to map each product ID to its corresponding price, allowing for quick price retrieval during the bill calculation. Each time a customer pays, we increment a counter. If the counter is divisible by n (i.e., every nth customer), we apply the given discount percentage to the computed subtotal. The solution is implemented in a class that provides both an initialization method and a bill calculation method.


Code Solutions

# Python solution

class Cashier:
    def __init__(self, n: int, discount: int, products: list, prices: list):
        # Store the discount interval and discount percentage.
        self.n = n
        self.discount = discount
        # Create a mapping from product id to its price.
        self.price_map = {}
        for i in range(len(products)):
            self.price_map[products[i]] = prices[i]
        # Initialize the customer counter.
        self.counter = 0

    def getBill(self, product: list, amount: list) -> float:
        # Increment the customer counter for each order.
        self.counter += 1
        # Calculate the total bill without discount.
        total = 0
        for i in range(len(product)):
            prod_id = product[i]
            qty = amount[i]
            total += self.price_map[prod_id] * qty
        # Apply discount if this is an nth customer.
        if self.counter % self.n == 0:
            total = total * ((100 - self.discount) / 100)
        return total

# Example usage:
# cashier = Cashier(3, 50, [1, 2, 3, 4, 5, 6, 7], [100, 200, 300, 400, 300, 200, 100])
# print(cashier.getBill([1,2], [1,2]))  # Output: 500.0
← Back to All Questions