Problem · Hash Table

Stock Marking Position Monitor

Learn this problem
MediumAkuna Capital logoAkuna CapitalFULLTIMEOA

Problem 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 integer delta to the number of held shares for symbol.
  • SELL,symbol,delta: add the signed integer delta to the total quantity of open sell orders for symbol.

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 SHARES or SELL.
  • Each symbol contains only uppercase English letters and has length from 1 through 10.
  • Each delta is a signed 32-bit integer.
  • After every event, held shares and open sell quantity for the affected symbol are both between 0 and 10^12, inclusive.

More Akuna Capital problems

drafts saved locally
public String[] markPositions(String[] events) {
    // Write your code here.
}
events["SHARES,AAPL,100","SELL,AAPL,30","SHARES,MSFT,50","SELL,AAPL,-10"]
expected["AAPL:100", "AAPL:70", "MSFT:50", "AAPL:80"]
checking account