Problem · Hash Table

Inventory Discount Tracker

Learn this problem
MediumTiktok logoTiktokOA
See Tiktok hiring insights

Problem statement

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.

Function

solution(pricelist: String[], logs: String[]) → int

Examples

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.

Constraints

  • 1 ≤ pricelist.length ≤ 100
  • Each element of pricelist is a string in the format "<item_name>: <price>" where item_name consists of alphanumeric characters and price is a positive integer.
  • All item names in pricelist are unique.
  • 1 ≤ logs.length ≤ 1000
  • Each element of logs is one of the following formats:
    • "sell <item_name>, <count>" where 1 ≤ count ≤ 1000
    • "discount_start <item_name>, <discount_amount>, <max_count>" where discount_amount is a positive integer and 1 ≤ max_count ≤ 100
    • "discount_end <item_name>"
  • All item_name values referenced in logs exist in pricelist.
  • There can be at most one active discount per item at any time.
  • discount_end <item_name> is only issued when item_name currently has an active discount.
  • All transactions in logs are sorted chronologically.
  • A discount_start for an item will not be issued if that item already has an active discount.

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
checking account