Apply Discount Every n Orders - Problem

๐Ÿ›’ Supermarket Cashier System

You're implementing a smart cashier system for a busy supermarket! The supermarket has decided to run an exciting promotion: every n-th customer gets a discount on their purchase.

Here's how it works:

  • Each product has a unique ID and a price
  • When customers check out, they bring multiple products with different quantities
  • Every n-th customer (3rd, 6th, 9th, etc.) gets a percentage discount on their total bill
  • Other customers pay the full amount

Your task: Design a Cashier class that tracks customer orders and applies discounts automatically!

Example: If n=3 and discount=50%, then the 3rd, 6th, 9th customers get 50% off their bills.

The class needs two methods:

  1. Cashier(n, discount, products[], prices[]) - Initialize the system
  2. getBill(product[], amount[]) - Calculate final bill for current customer

Input & Output

example_1.py โ€” Basic Usage
$ Input: Cashier cashier = new Cashier(3, 50, [1,2,3,4,5,6,7], [100,200,300,400,500,600,700]); cashier.getBill([1,2], [1,2]); // Customer 1 cashier.getBill([3,7], [10,10]); // Customer 2 cashier.getBill([1,2,3,4,5,6,7], [1,1,1,1,1,1,1]); // Customer 3
โ€บ Output: 500.0 10000.0 1400.0
๐Ÿ’ก Note: Customer 1: 1ร—100 + 2ร—200 = 500 (no discount). Customer 2: 10ร—300 + 10ร—700 = 10000 (no discount). Customer 3: 100+200+300+400+500+600+700 = 2800, but gets 50% discount = 1400.
example_2.py โ€” Small Discount
$ Input: Cashier cashier = new Cashier(2, 10, [1,2], [5,3]); cashier.getBill([1], [2]); // Customer 1 cashier.getBill([2], [3]); // Customer 2
โ€บ Output: 10.0 8.1
๐Ÿ’ก Note: Customer 1: 2ร—5 = 10 (no discount). Customer 2: 3ร—3 = 9, gets 10% discount = 9ร—0.9 = 8.1.
example_3.py โ€” Multiple Discount Customers
$ Input: Cashier cashier = new Cashier(3, 25, [1,2,3], [10,20,30]); cashier.getBill([1], [1]); // Customer 1 cashier.getBill([2], [1]); // Customer 2 cashier.getBill([3], [1]); // Customer 3 (discount) cashier.getBill([1,2], [1,1]); // Customer 4 cashier.getBill([2,3], [2,1]); // Customer 5 cashier.getBill([1,2,3], [1,1,1]); // Customer 6 (discount)
โ€บ Output: 10.0 20.0 22.5 30.0 70.0 45.0
๐Ÿ’ก Note: Customers 3 and 6 get 25% discount. Customer 3: 30ร—0.75=22.5. Customer 6: (10+20+30)ร—0.75=45.0.

Visualization

Tap to expand
๐Ÿ›’ Smart Cashier System๐Ÿช Store SetupPrice Map Created:๐ŸŽ Apple โ†’ $2๐Ÿฅ› Milk โ†’ $5Rule: Every 3rd = 20% offโšก Fast CheckoutCustomer #3 Cart:๐ŸŽ ร— 3 โ†’ $2 ร— 3 = $6๐Ÿฅ› ร— 2 โ†’ $5 ร— 2 = $10Subtotal: $16๐ŸŽ‰ Smart DiscountCustomer #3 Check:3 % 3 == 0 โœ“Apply 20% discount!Final: $16 ร— 0.8 = $12.80โฑ๏ธ Performance ComparisonBrute ForceSearch entire catalogTime: O(m ร— k)Hash MapInstant price lookupTime: O(k)๐Ÿ’ฐ Customer Counter: Simple modulo check for discount eligibility๐Ÿš€ Result: Lightning-fast checkout with automatic discounts!
Understanding the Visualization
1
System Setup
Store creates price lookup table and sets promotion rules (every nth customer gets discount)
2
Fast Checkout
Customer arrives with cart - system instantly looks up each item's price using hash map
3
Smart Discount
System checks customer count and automatically applies discount if they're the lucky nth customer
Key Takeaway
๐ŸŽฏ Key Insight: Hash maps transform O(m) searches into O(1) lookups, making the cashier system blazingly fast even with large product catalogs!

Time & Space Complexity

Time Complexity
โฑ๏ธ
O(k)

k is number of items in customer's cart - each price lookup is O(1) with hash map

n
2n
โœ“ Linear Growth
Space Complexity
O(m)

m is number of products in catalog - we store all product-price pairs in hash map

n
2n
โœ“ Linear Space

Constraints

  • 1 โ‰ค n โ‰ค 104
  • 0 โ‰ค discount โ‰ค 100
  • 1 โ‰ค products.length โ‰ค 200
  • 1 โ‰ค products[i] โ‰ค 200
  • 1 โ‰ค prices[i] โ‰ค 1000
  • products contains unique values
  • 1 โ‰ค product.length โ‰ค products.length
  • product[i] exists in products
  • 1 โ‰ค amount[i] โ‰ค 1000
  • At most 300 calls will be made to getBill
Asked in
Amazon 45 Google 32 Microsoft 28 Meta 22
42.3K Views
Medium Frequency
~15 min Avg. Time
1.9K Likes
Ln 1, Col 1
Smart Actions
๐Ÿ’ก Explanation
AI Ready
๐Ÿ’ก Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen