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.