Problem · Hash Table

Inventory Discount Tracker

Learn this problem
MediumCapital One logoCapital OneINTERNOA

Problem statement

Implement a simplified inventory tracker for a large retail store. You are given a price list pricelist that describes the current market price for each item and a chronological transaction log logs. Process all transactions and return the total revenue from all sales.

Price list

Each price-list entry has the format "<item_name>: <price>", which means that item_name has the regular unit price price.

Transactions

Each log entry has one of these formats:

  • "sell <item_name>, <count>": sell count units of item_name.
  • "discount_start <item_name>, <discount_amount>, <max_count>": start a discount for item_name. During this discount, at most max_count units in total are sold for price - discount_amount. Any additional units are sold for the regular price. The discounted quota is consumed across sales until the discount ends.
  • "discount_end <item_name>": end the active discount for item_name. The item is guaranteed to have an active discount at this point.

There is at most one active discount for each item at any time. A discount remains active after its quota is exhausted, but all later units use the regular price until discount_end.

Return the total revenue as an integer.

A solution with time complexity no worse than O(logs.length^2 * pricelist.length) fits within the execution 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

Process the sales in order. The first three sales contribute 100, 200, and 400. The discount on item2 applies to one unit of its next two-unit sale, so that sale contributes 160 + 200 = 360. The final two sales contribute 100 and 200. Therefore the total is 100 + 200 + 400 + 360 + 100 + 200 = 1360.

Constraints

  • 1 ≤ pricelist.length ≤ 100
  • Each element of pricelist has the format "<item_name>: <price>", where item_name contains only alphanumeric characters and price is a positive integer.
  • All item names in pricelist are unique.
  • 1 ≤ logs.length ≤ 1000
  • For every sell entry, 1 ≤ count ≤ 1000.
  • For every discount_start entry, discount_amount is a positive integer and 1 ≤ max_count ≤ 100.
  • Every item referenced in logs exists in pricelist.
  • There is at most one active discount per item. A discount_start is issued only when that item has no active discount, and a discount_end is issued only when it has one.
  • All entries in logs are sorted chronologically.
  • The total revenue fits in a signed 32-bit integer.

More Capital One 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