Apply Discount Every n Orders - Problem

There is a supermarket that is frequented by many customers. The products sold at the supermarket are represented as two parallel integer arrays products and prices, where the ith product has an ID of products[i] and a price of prices[i].

When a customer is paying, their bill is represented as two parallel integer arrays product and amount, where the jth product they purchased has an ID of product[j], and amount[j] is how much of the product they bought. Their subtotal is calculated as the sum of each amount[j] * (price of the jth product).

The supermarket decided to have a sale. Every nth customer paying for their groceries will be given a percentage discount. The discount amount is given by discount, where they will be given discount percent off their subtotal. More formally, if their subtotal is bill, then they would actually pay bill * ((100 - discount) / 100).

Implement the Cashier class:

  • Cashier(int n, int discount, int[] products, int[] prices) Initializes the object with n, the discount, and the products and their prices.
  • double getBill(int[] product, int[] amount) Returns the final total of the bill with the discount applied (if any). Answers within 10⁻⁵ of the actual value will be accepted.

Input & Output

Example 1 — Basic Cashier Operations
$ Input: [["Cashier",[3,50,[1,2,3,4,5,6,7],[100,200,300,400,300,200,100]]], ["getBill",[[1,2],[1,2]]], ["getBill",[[3,7],[10,10]]], ["getBill",[[1,2,3,4,5,6,7],[1,1,1,1,1,1,1]]]]
Output: [null,500.0,4000.0,800.0]
💡 Note: Cashier initialized with n=3, discount=50%. First customer: 1×100 + 2×200 = 500. Second customer: 10×300 + 10×100 = 4000. Third customer gets 50% discount: (1×100 + 1×200 + 1×300 + 1×400 + 1×300 + 1×200 + 1×100) × 0.5 = 800.
Example 2 — Every 2nd Customer Discount
$ Input: [["Cashier",[2,10,[1,2],[5,10]]], ["getBill",[[1],[2]]], ["getBill",[[2],[1]]]]
Output: [null,10.0,9.0]
💡 Note: Every 2nd customer gets 10% discount. First customer: 2×5 = 10 (no discount). Second customer: 1×10 = 10, with 10% discount = 9.0.
Example 3 — Single Product Purchase
$ Input: [["Cashier",[1,20,[1],[15]]], ["getBill",[[1],[3]]]]
Output: [null,36.0]
💡 Note: Every customer gets 20% discount (n=1). Customer buys 3 units of product 1: 3×15 = 45, with 20% discount = 45×0.8 = 36.0.

Constraints

  • 1 ≤ n ≤ 104
  • 0 ≤ discount ≤ 100
  • 1 ≤ products.length = prices.length ≤ 200
  • 1 ≤ products[i] ≤ 200
  • 1 ≤ prices[i] ≤ 1000

Visualization

Tap to expand
Apply Discount Every n Orders INPUT Product Price Map ID:1 --> $100 ID:2 --> $200 ID:3 --> $300 ID:4 --> $400 ID:5 --> $300 ID:6 --> $200 ID:7 --> $100 Parameters n = 3 discount = 50% getBill Calls: 1: [1,2], [1,2] 2: [3,7], [10,10] 3: [1-7], [1,1,1,1,1,1,1] Customer Counter: tracks nth customer ALGORITHM STEPS 1 Build Price HashMap Map product ID to price 2 Increment Counter counter++ on each getBill 3 Calculate Subtotal sum(amount[j] * price[j]) 4 Apply Discount if counter % n == 0 Discount Formula: bill * (100 - discount) / 100 bill * (100 - 50) / 100 Example: Call 3 (nth customer) Subtotal: 100+200+300+400+ 300+200+100 = 1600 FINAL RESULT Output for Each Call: Call 1 (Customer #1) 1*100 + 2*200 = 500 500.0 Call 2 (Customer #2) 10*300 + 10*100 = 4000 4000.0 Call 3 (Customer #3) - DISCOUNT! Subtotal: 1600 1600 * 50% = 800 800.0 Final Output Array: [null, 500.0, 4000.0, 800.0] Key Insight: Use a HashMap for O(1) product price lookup. Track customer count with a simple counter. Apply discount when counter % n == 0. Reset counter or use modulo to handle cycles efficiently. TutorialsPoint - Apply Discount Every n Orders | Hash Map Approach
Asked in
Amazon 35 Google 28 Microsoft 22
25.4K Views
Medium Frequency
~15 min Avg. Time
892 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