Problem · Hash Table

Client Stock Position Operations

Learn this problem
MediumOptiver logoOptiverFULLTIMEPHONE SCREEN

Problem statement

Process every command in operations in order. Each client starts with a position of zero shares in every symbol.

  • BUY:client:symbol:quantity: add quantity shares to that client-symbol position and output the resulting quantity as a decimal string.
  • SELL:client:symbol:quantity: if the position contains at least quantity shares, subtract them and output the resulting quantity. Otherwise, leave the position unchanged and output REJECTED.
  • POSITION:client:symbol: output the current quantity, or 0 if that position has never been traded.

Return one output for every command. Positions belonging to different clients or symbols are independent.

Function

processStockPositions(operations: String[]) → String[]

Examples

Example 1

operations = ["BUY:alice:AAPL:10","BUY:alice:AAPL:5","SELL:alice:AAPL:8","POSITION:alice:AAPL"]return = ["10","15","7","7"]

Alice's AAPL position grows to 15, then a successful sale reduces it to 7.

Example 2

operations = ["BUY:bob:MSFT:4","SELL:bob:MSFT:5","POSITION:bob:MSFT","POSITION:bob:GOOG"]return = ["4","REJECTED","4","0"]

Bob cannot sell five MSFT shares while holding four, so the rejected sale leaves the position unchanged. An unseen symbol has position zero.

Example 3

operations = ["BUY:alice:NVDA:3","BUY:bob:NVDA:6","SELL:alice:NVDA:3","POSITION:alice:NVDA","POSITION:bob:NVDA"]return = ["3","6","0","0","6"]

Alice's and Bob's NVDA positions are independent. Alice sells her entire position while Bob continues to hold six shares.

Constraints

  • 1 <= operations.length <= 100000
  • Every operation is exactly one of the documented command forms.
  • Each client and symbol is 1 to 24 ASCII letters, digits, underscores, or hyphens.
  • 1 <= quantity <= 1000000000 for BUY and SELL.
  • Every resulting position fits in a signed 64-bit integer.

More Optiver problems

drafts saved locally
public String[] processStockPositions(String[] operations) {
  // write your code here
}
operations["BUY:alice:AAPL:10","BUY:alice:AAPL:5","SELL:alice:AAPL:8","POSITION:alice:AAPL"]
expected["10", "15", "7", "7"]
checking account