Problem · Hash Table

Inventory Discount Tracker

MediumTiktokOA
See Tiktok hiring insights

Your task is to implement a simplified inventory tracker system for a large retail store. You are given a price list pricelist that describes the current market price for each item, and a transaction log logs where each record contains information about one of three types of transactions: sell, discount_start, or discount_end. The tracker should process all transactions and return the total revenue from all sales.

Each element in the price list follows this format: "<item_name>: <price>", which represents that <item_name> can be sold at the price of <price>.

Transactions are provided in the following format:

  • "sell <item_name>, <count>": the store sells <count> units of <item_name>.
  • "discount_start <item_name>, <discount_amount>, <max_count>": the store announces a discount, and now sells <item_name> at the price of price - <discount_amount>. However, only <max_count> units can be sold at the discounted price, and all additional units are sold at the original price. <max_count> can be at most 100, and there can only be one discount for each item at any time.
  • "discount_end <item_name>": the store announces the end of the discount for <item_name>. It is guaranteed that the item has a discount at this point.

Note: All transactions are sorted chronologically.

Note: You are not expected to provide the most optimal solution, but a solution with time complexity not worse than O(logs.length2 * pricelist.length) will fit within the execution time limit.

Examples
01 · Example 1
pricelist = ["item1: 100", "item2: 200"]
logs = ["sell item1, 1", "sell item1, 2", "sell item2, 2", "discount_start item2, 40, 1", "sell item2, 2", "sell item1, 1", "discount_end item2", "sell item2, 1"]
return = 1360

The transactions are processed in chronological order:

  • "sell item1, 1": revenue is 1 * 100 = 100.
  • "sell item1, 2": revenue is 2 * 100 = 200.
  • "sell item2, 2": revenue is 2 * 200 = 400.
  • "discount_start item2, 40, 1": item2 now has a discount of 40 for at most 1 unit.
  • "sell item2, 2": one unit is sold at 200 - 40 = 160, and the other unit is sold at the original price of 200, so revenue is 160 + 200 = 360.
  • "sell item1, 1": revenue is 1 * 100 = 100.
  • "discount_end item2": the discount for item2 ends.
  • "sell item2, 1": revenue is 1 * 200 = 200.

The total revenue is 100 + 200 + 400 + 360 + 100 + 200 = 1360.

More Tiktok problems
drafts saved locally
public int solution(String[] pricelist, String[] logs) {
  // write your code here
}
pricelist["item1: 100", "item2: 200"]
logs["sell item1, 1", "sell item1, 2", "sell item2, 2", "discount_start item2, 40, 1", "sell item2, 2", "sell item1, 1", "discount_end item2", "sell item2, 1"]
expected1360
sign in to submit