Problem · Hash Table
Stock Marking Position Monitor
Learn this problemProblem statement
Process a stream of events while maintaining independent state for every stock symbol.
Each event is one of the following comma-separated records:
SHARES,symbol,delta: add the signed integerdeltato the number of held shares forsymbol.SELL,symbol,delta: add the signed integerdeltato the total quantity of open sell orders forsymbol.
All symbols begin with zero held shares and zero open sell quantity. After applying each event, compute that symbol's marking position:
marking position = held shares - open sell quantity
Return one record for every input event, in the same order, formatted as symbol:markingPosition.
Function
markPositions(events: String[]) → String[]Examples
Example 1
events = ["SHARES,AAPL,100","SELL,AAPL,30","SHARES,MSFT,50","SELL,AAPL,-10"]return = ["AAPL:100","AAPL:70","MSFT:50","AAPL:80"]The two symbols have separate state. The final event reduces the open AAPL sell quantity from 30 to 20, so its marking position becomes 100 - 20 = 80.
Example 2
events = ["SHARES,NVDA,12","SELL,NVDA,12","SELL,NVDA,-5","SHARES,NVDA,-2"]return = ["NVDA:12","NVDA:0","NVDA:5","NVDA:3"]Signed deltas model later adjustments. After all four events, held shares are 10 and open sell quantity is 7.
Constraints
1 <= events.length <= 10^5.- Every event has exactly three comma-separated fields and begins with
SHARESorSELL. - Each symbol contains only uppercase English letters and has length from
1through10. - Each delta is a signed 32-bit integer.
- After every event, held shares and open sell quantity for the affected symbol are both between
0and10^12, inclusive.