Problem · Hash Table
Robot Inventory Tracking
Learn this problemProblem statement
Implement an inventory tracking system for a robot retail store. You are given a transaction log logs. Process every record in order.
Each record has one of these formats:
["supply", robotName, count, price]: addcountunits ofrobotNameto inventory at unit priceprice.["sell", robotName, count]: sellcountunits ofrobotName. If units are available at different prices, sell the cheapest units first.["upgrade", robotName, count, oldPrice, newPrice]: movecountunits ofrobotNamefromoldPriceto the higher pricenewPrice.
The count and price fields are decimal strings. Return an integer array containing the revenue from each sell transaction in the order those transactions appear.
A solution with time complexity no worse than O(logs.length^2) fits within the execution time limit.
Function
trackRobotInventory(logs: String[][]) → int[]Examples
Example 1
logs = [["supply","robot1","2","100"],["supply","robot2","3","60"],["sell","robot1","1"],["sell","robot2","1"],["upgrade","robot2","1","60","100"],["sell","robot2","1"],["sell","robot2","1"]]return = [100,60,60,100]- The two
supplyrecords add tworobot1units at price100and threerobot2units at price60. - Selling one
robot1produces revenue100. - Selling one
robot2produces revenue60. - The
upgraderecord moves one remainingrobot2unit from price60to price100. - The next
robot2sale uses the remaining price-60unit first, producing revenue60. - The final
robot2sale uses the price-100unit, producing revenue100.
The revenues are therefore [100,60,60,100].
Constraints
- Every
selltransaction can be fulfilled by the current inventory. - Every
upgradetransaction has at leastcountmatching units atoldPrice. - For every
upgradetransaction,newPriceis higher thanoldPrice.
More Airbnb problems
- Most Frequent Reduced DigitOA · Seen Jul 2026
- Rectangle Fit QueriesOA · Seen Jul 2026
- Robot Final DirectionOA · Seen Jul 2026
- Parse Query StringPHONE SCREEN · Seen May 2026
- Print Sentences as TablePHONE SCREEN · Seen May 2026
- Minimum Eating SpeedSeen Mar 2024
- Minimum MovesSeen Dec 2022
- Donut ChallengeSeen Dec 2022