Worst Trade Reporter
Problem statement
Process a stream of price updates, trades, and worst-trade queries.
PRICE instrument pricesets the latest true price for an instrument.TRADE tradeId instrument BUY|SELL price volumerecords a unique trade.WORST_TRADE instrumentasks for the recorded trade with the greatest loss per lot at the latest true price.
For a buy, profit per lot is truePrice - tradePrice. For a sell, profit per lot is tradePrice - truePrice. A trade is bad only when this value is negative. If several trades have the same greatest loss per lot, choose the one that appeared latest in the instruction stream. If no trade is bad, return NO BAD TRADES.
Return one string for every query, in query order.
Function
worstTrades(instructions: String[]) → String[]Examples
Example 1
instructions = ["PRICE Facebook 80","PRICE Apple 120","TRADE 100 Apple SELL 90 2","TRADE 10 Facebook BUY 100 4","WORST_TRADE Facebook","WORST_TRADE Apple"]return = ["10","100"]The Facebook buy loses 20 per lot, while the Apple sell loses 30 per lot.
Example 2
instructions = ["PRICE Google 100","TRADE 1 Google BUY 100 10","WORST_TRADE Google","TRADE 2 Google SELL 102 5","TRADE 3 Google SELL 103 5","PRICE Google 98","WORST_TRADE Google","TRADE 4 Google BUY 101 10","TRADE 5 Google BUY 100 10","WORST_TRADE Google"]return = ["NO BAD TRADES","1","4"]The first query has no loss. At price 98, trade 1 loses 2 per lot; after two more buys, trade 4 loses 3 per lot.
Constraints
1 <= instructions.length <= 10^61 <= tradeId, price, volume <= 10^6- Every trade ID is unique.
- A price update for an instrument appears before its first trade or query.
- Instrument identifiers contain no spaces.